简体   繁体   中英

Javascript Regex to ignore first character in match

I need to ignore a '.' at the start of my regular expression, and have been somewhat stumped.

My current regex is:

(?::)(\d{3})

Which matches the following:

图片在这里

When I try to ignore the '.' with the following regex:

[^.](?::)(\d{3})

I get this:

想象一下

As it seems to be adding the extra character like '<', which is unwanted.

How do I go about to ignore that extra character in front of the ':' ?

Use this alternation based regex:

\.:\d{3}|:(\d{3})

And grab captured group #1 for your matches.

RegEx Demo

Just use a lookahead to match the strings in this :\\d{3} format preceded by any but not of dot.

(?=[^.](:(\d{3})))

DEMO

Group 1 contains the string with : , and the group 2 contains only the digits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM