Rekovni
Rekovni

Reputation: 7354

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:

Image here

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

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

I get this:

Picture here

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

Upvotes: 4

Views: 10402

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174696

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.

Upvotes: 1

anubhava
anubhava

Reputation: 785038

Use this alternation based regex:

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

And grab captured group #1 for your matches.

RegEx Demo

Upvotes: 2

Related Questions