Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?.*/;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...