简体   繁体   中英

Regular expression issue when match a string in javascript

I am very, very n00b in regular expression, I'm trying to learn but no success :).

So I have the following file name: myFile.8.9.6-x64.txt or myFile.8.9.6-x86.txt

So, I want to create a regular expression in order to match x64 or x86 string in file name with:

var regexp = new RegExp(/[x_X][8][6]|[x][6][4]$/);

console.log(regexp.test("myFile.8.9.6-x64.txt")); //returns false instead of true

So, where is my mistake ?

You can actually use this regex:

var regexp = /x(?:86|64)\./i;
  • new RegExp takes a string actually not a regex with delimiters as shown in your question
  • You regex has $ after 64/86 but your file names are not ending with 64/86 .
  • No need to repeat x
  • You can use /i for ignore case matching

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