简体   繁体   English

在javascript中匹配字符串时的正则表达式问题

[英]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 :). 我在正则表达式中非常非常n00b ,我正在尝试学习但没有成功:)。

So I have the following file name: myFile.8.9.6-x64.txt or myFile.8.9.6-x86.txt 因此,我具有以下文件名: myFile.8.9.6-x64.txtmyFile.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: 因此,我想创建一个正则表达式,以便将文件名中的x64x86字符串与以下内容匹配:

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 new RegExp接受的字符串实际上不是带分隔符的正则表达式,如您的问题所示
  • You regex has $ after 64/86 but your file names are not ending with 64/86 . 您的正则表达式在64/86之后有$ ,但是文件名不是以64/86结尾。
  • No need to repeat x 无需重复x
  • You can use /i for ignore case matching 您可以使用/i忽略大小写匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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