clarkk clarkk - 1 month ago 12
Javascript Question

JavaScript String replace - How do you use matched variables in the replacement string?

How do you use the matched variables in the pattern in the replacement string?

var regexp = new RegExp('needle', 'ig');
str.replace(regexp, '<span class="marked">//1</span>')

Answer

try

var regexp = new RegExp(something, 'ig');
str.replace(regexp, '<span class="marked">$&</span>')

References:

Comments