简体   繁体   English

Javascript Regex忽略匹配中的第一个字符

[英]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 RegEx演示

Just use a lookahead to match the strings in this :\\d{3} format preceded by any but not of dot. 只需使用前瞻匹配字符串:\\d{3}格式前面有任何但不是点。

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

DEMO DEMO

Group 1 contains the string with : , and the group 2 contains only the digits. 组1包含带有:的字符串,组2仅包含数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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