1

I don't have much knowledge about working of regex but I have been trying to solve this from last many hours but could not get solution of it`.

regex  /^[]{13,17}$/i

I have a text-box and text-box value should be greater than or equal to 13 and less than or equal to 17.

Ex.

var value - "12345678901234".(textbox value and length is 14)
if (value.match(regex)) {
  alert("Correct value");
} else {
  alert("error");
}

Now, length is 14, it means condition should be true. But match function always return null. I have tried also test function, but it doesn't give desired result.

11
  • 2
    Use ^.{13,17}$. [] means nothing. Commented Feb 9, 2016 at 10:04
  • Hi Wiktor. This regex is coming from database and it's creating dynamically. In subscript, sometimes value can't be blank. Commented Feb 9, 2016 at 10:05
  • Then you have to replace dynamically [] by . ; check alert("1234567890123".match( /^[]{13,17}$/i) + " " + "1234567890123".match( /^.{13,17}$/i)) and you will see the difference. Commented Feb 9, 2016 at 10:10
  • This online regex expression builder might help. It displays errors and provides expression hints: https://regex101.com/ Commented Feb 9, 2016 at 10:12
  • If you actually mean any character, then you must replace [] with [^] or [\s\S]. Can you replace each [] with [\s\S]/[^]? Commented Feb 9, 2016 at 10:12

1 Answer 1

3

You should specify which kind of characters has to be in the sequence:

Any character-

/^.{13,17}$/i

Digits -

/^[0-9]{13,17}$/i

Letters -

/^[A-Z]{13,17}$/i

letters, digits, underscore and dash -

/^[A-Z0-9_-]{13,17}$/i
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Puneet - I agree with other comment contributions - if that regular expression is given, you need to change it dynamically (sorry, but when I started answering there was no comment).
Okay. Can you please update your question with specifying how can i change it dynamically.
I am not sure about your case, but you can convert the regexp to a string (toString()), strip the leading '/' and trailing '/i' away, then replace the squares with a dot replace( "[]", "." ) and eventually convert the string back to regexp (new RegExp( str, "i" ))
It also doesn't work - Any character - /^.{13,17}$/i. Only this work ^.{13,17}$ but can't use this.
I think you are mixing strings ("abc") and regexp (/abc/) concepts. If you invoke match with a string, then no slashes are needed, but if you use a regexp, then slashes are needed because they are the delimiters for the regexp itself. You can switch back and forth between regexp and string as I wrote in my comment above.

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.