Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  174] [ 4]  / answers: 1 / hits: 26342  / 12 Years ago, tue, november 6, 2012, 12:00:00

I'd like to replace character inside a string, e.g.



Drafts [2]



To:



Drafts [3]



This regex returns only Drafts 3:



str.replace(/[(.+?)]/g, 3)


Thanks for help in advance


More From » replace

 Answers
39

Do you need something more than below?



 var num=2 // parse this from drafts [2]
num++;
var newstr=str.replace(/[(.+?)]/g, [+num+])


Or the brackets can change to <> {} per input?



You can also give a function instead of the replace-string.



var str = Drafts [2];

function replacer(match, p1, p2, p3, offset, string) {
return p1 + (1+parseInt(p2)) + p3;
}
var newstr=str.replace(/([[(])(.+?)([])])/g, replacer);
alert(newstr); // alerts Drafts [3]

[#82165] Monday, November 5, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jazminuniquer

Total Points: 63
Total Questions: 121
Total Answers: 96

Location: Cambodia
Member since Thu, May 21, 2020
4 Years ago
;