The string can be http://url.com/scripts.js:129:1 or http://url.com/scripts.js:129 but I always want http://url.com/scripts.js:129.
Here is what I tried, but it keeps changing array index.
'http://url.com/scripts.js:129:1'.match(/(.+)(\:\d+(?!\:\d+)?)$/);
Help me, why my regex is not working properly.
If the point is to get till the first : + number and omit the rest, you can use
^(.+?:\d+)(?::\d+)*$
^.+?:\d+
See this regex demo. ^.+?:\d+ is preferable if you do not need to match the entire string and capture both parts into groups.
Details:
^ - start of string(.+?:\d+) - Group 1 capturing any one or more chars other than line break chars as few as possible and then a : and one or more digits, and then(?::\d+)* - zero or more sequences of : and one or more digits$ - end of string.Try the following pattern:
/:(\d+)(?::\d+)?$/
It assumes that there may be another colon-number pair, but it's optional.