This works well for case insensitive replacements:
str = str.replace(new RegExp(phrase, 'gi'), '<span style="color:red;">' + phrase + '</span>');
But what I do want is to not change case when replaced which above-mentioned does.
This works well for case insensitive replacements:
str = str.replace(new RegExp(phrase, 'gi'), '<span style="color:red;">' + phrase + '</span>');
But what I do want is to not change case when replaced which above-mentioned does.
Capture the phrase with parenthesis, and use $1 in the replace string, eg:
'Foobar'.replace(/(foo)/gi, '<x>$1</x>')
Would result in <x>Foo</x>bar