简体   繁体   中英

RegExp.test returns false even though it should be true

I'm trying to write this simple code into chrome live test and can't get it to return true.

var regExS = new RegExp("\d+x\d+");
regExS.test(" 240x120 (399.00)");

Even if I change the value to "240x120" it returns false. I've been googling and looking around and can't solve it.

If you are creating a RegExp from a string, the backslashes will need to be escaped ( "\\d" is the same as "d" ):

var regExS = new RegExp("\\d+x\\d+");

Alternatively you can use a regular expression literal:

var regExS = /\d+x\d+/;

The result of var regExS = new RegExp("\\d+x\\d+"); is /d+xd+/ .
You need to escape the backslashes when building a regex from a string:

var regExS = new RegExp("\\d+x\\d+");

or you could use a regex literal

var regExS = /\d+x\d+/;

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