javascript - new RegExp is not working - Stack Overflow

I have a regular expression which validates 3 consecutive digits.^d{3}$.test("12345")

I have a regular expression which validates 3 consecutive digits.

/^\d{3}$/.test("12345")  // false
/^\d{3}$/.test("123")    // true

How can I transform this regex pattern into a RegExp object?

I tried:

var re = new RegExp("\\d{3}", "gi");

but re.test("12345") returns true

What am I doing wrong?

I have a regular expression which validates 3 consecutive digits.

/^\d{3}$/.test("12345")  // false
/^\d{3}$/.test("123")    // true

How can I transform this regex pattern into a RegExp object?

I tried:

var re = new RegExp("\\d{3}", "gi");

but re.test("12345") returns true

What am I doing wrong?

Share Improve this question edited Jan 30, 2012 at 7:53 James Allardice 166k22 gold badges334 silver badges315 bronze badges asked Jan 30, 2012 at 7:48 CatalinCatalin 11.7k19 gold badges80 silver badges152 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5
var re = new RegExp("^\\d{3}$", "gi");

(I assume the "gi" flag is not really necessary in this case...)

http://jsfiddle/GyZqw/

new RegExp("^\\d{3}$", "gi")

you forgot ^ and $

Use this regular expression:

^\d{3}$

with start and end of line specified.

In JavaScript you should escape \ char, i.e.:

"^\\d{3}$"
var re = new RegExp("^\\d{3}$");

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744685842a4587910.html

相关推荐

  • javascript - new RegExp is not working - Stack Overflow

    I have a regular expression which validates 3 consecutive digits.^d{3}$.test("12345")

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信