0

I my application I am using below regex for pattern matching.

Original Pattern :

/(\w+\.){2,}/ig

Above pattern added in one array. Since this pattern has comma ( , ) after 2, creating problem in some environment.

As we know below concept in regex :

 {n} - matches n times
 {n, m} - matches at least n times, but not more than m times

So I have removed comma present after 2, because in above pattern no value exist after comma.

Pattern after removing comma :

/(\w+\.){2}/ig

As per above change i have resolved environment problem which i was facing earlier.

So here, I just wanted to know that by removing comma after 2 creates any problem while matching, for above given case.

2 Answers 2

1

{2} means match if it appears exactly 2 times, and {2,} means 2 times or above. Depending on the usage, this may or may not matter.

For example, if you want to validate whether the string contains 2 or more \w+\., then the comma doesn't matter. However, if you want to replace those 2 or more \w+\. with something else, the comma will affect the result.

'foo.bar.baz.'.replace(/(\w+\.){2}/ig, '~') == '~baz.'
'foo.bar.baz.'.replace(/(\w+\.){2,}/ig, '~') == '~'
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Kenny your info helps me lot
1

{2,} means two or more. There is no max limit. With this, {0,} is the same as *, and {1,} is the same as +

To summarize:

{n} match n times
{n,m} match at least n times, but not more than m times
{n,} match at least n times

Refer this for details

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.