简体   繁体   中英

Javascript regular expression to check if the file ends with (.csv)

I want to select the last .csv in text.csv.csv I tried (\\.csv)$ but it didn't work.

I am using this site to test my regax https://regex101.com/#javascript

Please try:

/\.csv$/

Added '\\' to escape the dot.

You need to use a backslash to escape the dot character, to check if the dot is present.

If you don't escape it, the dot represents a single character, so, for example, 'test.csv.xxxcsv' would match too.

Working example

Why do you need regex for this? Simple JavaScript functions should suffice

var str = "myFileName.csv";
var index = str.lastIndexOf(".csv");

var len = str.length;
var res = "";
if(len - index  == 4) 
{
res = "CSV Found"
} 
else {
res = "CSV Not Found"
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM