js regex replace multiple words
Posted
by
Raghav
on Stack Overflow
See other posts from Stack Overflow
or by Raghav
Published on 2012-12-19T16:51:43Z
Indexed on
2012/12/19
17:03 UTC
Read the original article
Hit count: 263
JavaScript
I need to replace ${conferance name} with ABC, ${conference day} with Monday in the following sentence. Could some one help me with the regex.
var text = "<td>${conference name}</td><td>${conference day}</td>"
var list = ["${conferance name}", "${conference day}" ]
for (var j = 1; j < list.length; j++) {
//Extracting the col name
var colName = list[j].split("${");
colName = colName.split("}")[0];
//Replacing the col name
text = text.replace(new RegExp('\\$\\{' + colName + '\\}', 'g'), "ABC");
}
The above code repalces fine if i have ${conference_name}, but it fails when i have a space in between.
The list is a dynamic array. And the Replace statements are also dynamic. I just simulated them as objects here for fitting them in the Regex Statement.
Thanks in Advance.
© Stack Overflow or respective owner