Snusmumriken
Snusmumriken

Reputation: 19

JavaScript regex for matching any number ending with zero or one occurrence of specific character

I need to match a string which has to begin with any number and followed by zero or one occurrence of 'w' or 'm' character. E.g. 123, 321w or 231m

'^[0-9]+$' works fine for just the number checking.

I guess something along the lines of [wm]{0,1} must be added to check for 0 or 1 occurrence of w or m?

Upvotes: 0

Views: 995

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

Use the following regex

/^\d+[wm]?$/

^ and $ - anchors for start and end position

\d+ - match any digit combination

[wm]? - match optional w or m at the end


Regex explanation here.

Regular expression visualization

Upvotes: 2

Related Questions