Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript RegExp test fails

I have problem with this regex:

(\[|\])[0-9]+,([0-9]+(\[|\])|inf\])\s?.*

Regular expression visualization

Debuggex Demo

When I try to do code:

var rangeRegex = new RegExp("(\[|\])[0-9]+,([0-9]+(\[|\])|inf\])\s?.*");
console.log(rangeRegex.test("]1,inf] Test Expression"));

I always get false. Why?

like image 623
michail_w Avatar asked Feb 19 '26 14:02

michail_w


1 Answers

When you use the RegExp construct, you need to double escape with your backslashes:

var rangeRegex = new RegExp("(\\[|\\])[0-9]+,([0-9]+(\\[|\\])|inf\\])\\s?.*");

Or use a literal one::

var rangeRegex = /(\[|\])[0-9]+,([0-9]+(\[|\])|inf\])\s?.*/;
like image 83
Jerry Avatar answered Feb 22 '26 02:02

Jerry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!