Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
>"123-456-7890".match(/(\d{3})-(\d{3})/)
<(3) ['123-456', '123', '456', index: 0, input: '123-456-7890', groups: undefined]

>"123-456-7890".match(/(\d{3})-\1/)
<null

// expecting 123-456


What I have tried:

I tried in different way but it don't work
Posted
Updated 16-Dec-22 2:01am
v3
Comments
Mike Hankey 16-Dec-22 7:59am    
Another good link I found a while back, don't remember where for learning RegEx;
https://regex101.com/

1 solution

It's not at all clear what you're trying to do, but your input clearly doesn't match the second pattern.

Your first pattern matches any three digits, followed by a "-", followed by any other three digits.

Your second patter matches any three digits, followed by a "-", followed by the same three digits again.

There are two possible places a match could occur: 123-456 and 456-789. Neither of those repeat the same three digits, so neither is a match for your second expression.

You need to read up about how regular expressions work:
Regular expressions - JavaScript | MDN[^]

And if you're going to be working with regular expressions, get yourself a copy of Expresso:
Download Expresso[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900