Sunday, 15 March 2015

Regex match for specific number before but not after in javascript -



Regex match for specific number before but not after in javascript -

i suck @ regex. i've been reading @ http://www.regular-expressions.info/tutorial.html can't figure out how write this.

i have string contains 2 numbers (days of month leading 0s). i'm trying remove leading 0s string not remove 0 in "10" or "20".

example strings here: "01","02","03","10","11","12","20","31"

since string day of month, 2 characters in length , between 01 , 31.

currently i'm using (which wrong):

string.replace(/0/,'');

what i'm trying end this: "1" instead of "01", "2" instead of "02", "10" without losing "0".

hopefully clear enough.

how do correctly?

if string contains number convert integer, eg:

var num = +str;

if want replace parts of larger string, can utilize \b:

str.replace(/\b0+\b/g, '');

example:

"i have 000100 , 0020!".replace(/\b0+\b/g, '')

returns:

"i have 100 , 20!"

javascript regex

No comments:

Post a Comment