0

Is there any difference between the regular expressions:-

/^[1-9][0-9]+$/

and

/^[1-9]{1}[0-9]+$/

They both seem to give the same result. Which convention is better,because in many examples I can see the use of {1}, but feel it is quite useless, am I right?

Parveez Ahmed
  • 1,325
  • 4
  • 17
  • 28
  • 2
    The `{1}` quantifier is completely unnecessary here. – Pointy Oct 03 '18 at 16:38
  • @Pointy I think in any case, {n} is meaningless when n=1, am I right? – Parveez Ahmed Oct 03 '18 at 16:40
  • 1
    I'm certainly having a hard time thinking of a case where it actually makes a difference in the meaning of the regex. – Pointy Oct 03 '18 at 16:42
  • 1
    There is no such case, it is the same. – Killer Death Oct 03 '18 at 16:42
  • 2
    From the [`regex` tag description](https://stackoverflow.com/tags/regex/info): *Using `{1}` as a single-repetition quantifier is harmless, but very often an indication of inexperience and/or confusion.* – Wiktor Stribiżew Oct 03 '18 at 16:43
  • 1
    As for confusion: Some people seem to think that by adding a `{1}`, the regex engine would interpret it as "match only if there is exactly one instance", so that `a{1}` would match `a` but not `aa`, which is not the case, of course. – Tim Pietzcker Oct 03 '18 at 16:56

0 Answers0