javascript - java script Regular Expressions patterns problem -
my problem start like-
var str='0|31|2|03|.....|4|2007' str=str.replace(/[^|]\d*[^|]/,'5'); so output becomes like:"0|5|2|03|....|4|2007" replaces 31->5
but doesn't work replacing other segments when change code this:
str=str.replace(/[^|]{2}\d*[^|]/,'6'); doesn't change 2->6. missing here.any help?
here specific regex solution replaces number following first | pipe symbol number 5:
var re = /^((?:\d+\|){1})\d+/; return text.replace(re, '$15'); if want replace digits following third |, change {1} portion of regex {3}
here generalized function replace given number slot (zero-based index), specified new number:
function replacenthnumber(text, n, newnum) { var re = new regexp("^((?:\\d+\\|){"+ n +'})\\d+'); return text.replace(re, '$1'+ newnum); }
Comments
Post a Comment