33

How can I limit the length of a string matching a RegEx

I assumed that var sixCharsRegEx = /^.{6,7}/ would only match strings of lengths 6 or 7

but no: http://jsfiddle.net/FEXbB/

What am I missing?

4 Answers 4

56

You are missing closing dollar at the end. Correct one is: /^.{6,7}$/

Sign up to request clarification or add additional context in comments.

1 Comment

$ in this case means, that the 6th or 7th char will the last one.
9

Match the start and the end.

var sixCharsRegEx = /^.{6,7}$/;

Your improved example

Comments

6

you must use end of string symbol $

like this ^.{6,7}$

Comments

5

You are missing the end anchor:

var sixCharsRegEx = /^.{6,7}$/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.