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>')
try
var regexp = new RegExp(something, 'ig');
str.replace(regexp, '<span class="marked">$&</span>')
References:
$&, $' and $` replacements.The correct way to use backreferences in JavaScript is via $1...$9.
To make your example work:
var regexp = new RegExp(something, 'ig');
var result = str.replace(regexp, '<span class="marked">$1</span>');
More information is available here: http://www.regular-expressions.info/javascript.html#replace
/(needle)/. Otherwise, use $& that Eineki shows.
//1? Read the documentation on the replace string more closely, especially the part about the special$variables.