regex - Simple Javascript replace with a loop -
I'm trying to change all the components together with the array index value with the string below.
var str = '& lt; A href = "{0}" title = "{1}" & gt; {1} & lt; / A & gt; '; Var params = []; Params.push ('url', 'titledisplay'); For (in the parameter) {var x = / '{' + i + '}' / g; Str = str.replace (x, parameter [i]); } No matter what I do, I can not seem to work on it Except '/ g' works with a match, but not all. I know that this is basic, but for me, I can not get it to work.
code:
var rx = / {([0- 9] +)} / g; Str = str.replace (rx, function ($ 0, $ 1) {return parameter [$ 1];}); Replace method loops via string (in Regex in / g) and find all examples of {n} where n is a number. Captures $ 1 number and changes the function {n} with parameter [n].
Comments
Post a Comment