clarkk clarkk - 9 months ago 49
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 Source


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