最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript Regex to ignore first character in match - Stack Overflow

programmeradmin7浏览0评论

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 ':' ?

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 ':' ?

Share Improve this question edited Sep 16, 2014 at 18:54 msrd0 8,46010 gold badges59 silver badges92 bronze badges asked Sep 16, 2014 at 18:23 RekovniRekovni 7,4225 gold badges50 silver badges68 bronze badges 6
  • Regex101 here: regex101./r/cE0wH4/1 – Rekovni Commented Sep 16, 2014 at 18:23
  • @hwnd may i know the reason? – Avinash Raj Commented Sep 16, 2014 at 18:29
  • : should be included. – hwnd Commented Sep 16, 2014 at 18:29
  • 1 You're going through a lot of effort to ignore a character. The way I ignore characters is I don't put it in the regex. – user557597 Commented Sep 16, 2014 at 18:30
  • I would say the answer is you have to match the character before the colon, no matter what it is, then check if it matched a dot . or not. May require some extra code. – user557597 Commented Sep 16, 2014 at 18:52
 |  Show 1 more ment

2 Answers 2

Reset to default 2

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.

发布评论

评论列表(0)

  1. 暂无评论