Regular expression to limit number of characters to 10

fiktionvt picture fiktionvt · Oct 30, 2009 · Viewed 461.1k times · Source

I am trying to write a regular expression that will only allow lowercase letters and up to 10 characters. What I have so far looks like this:

pattern: /^[a-z]{0,10}+$/ 

This does not work or compile. I had a working one that would just allow lowercase letters which was this:

pattern: /^[a-z]+$/ 

But I need to limit the number of characters to 10.

Answer

cletus picture cletus · Oct 30, 2009

You can use curly braces to control the number of occurrences. For example, this means 0 to 10:

/^[a-z]{0,10}$/

The options are:

  • {3} Exactly 3 occurrences;
  • {6,} At least 6 occurrences;
  • {2,5} 2 to 5 occurrences.

See the regular expression reference.

Your expression had a + after the closing curly brace, hence the error.