Friday, 15 March 2013

javascript - Regex for two digit number -



javascript - Regex for two digit number -

if do:

var pagenum= 2; nextlink = http://mydomain.com/?paged=1 nextlink = nextlink.replace(/\/?paged=[0-9]?/, 'paged='+ pagenum);

i

nextlink = 'http://mydomain.com/?paged=2';

but if go:

var pagenum= 12; nextlink = http://mydomain.com/?paged=10 nextlink = nextlink.replace(/\/?paged=[0-9]?/, 'paged='+ pagenum);

i

netlink = 'http://mydomain.com/?paged=120';

when want:

nextlink = 'http://mydomain.com/?paged=12';

so, how can utilize 2 digit number (11, 12, 13, 14 etc) , replace pagenum variable (which can both 1 , 2 digit number).

thank's!

your code be,

nextlink = nextlink.replace(/paged=[0-9]+/, 'paged='+ pagenum);

you failed set +, symbol means 1 or more times match 1 or more numbers next string paged=. , remove ? quantifier shortest match.

demo

javascript regex

No comments:

Post a Comment