var str = 'test TEST';
How to get str = '<span class="red">test</span> <span class="red">TEST</span>' in return.
Thanks a lot.
The following will work for words composed of characters matched by \w, which are a-z, A-Z, 0-9 and _. This means, for example, that it will fail for words containing accented characters, but will work fine for your example.
var str = 'test TEST';
var highlighted = str.replace(/([\w]+)/g, '<span class="red">$1</span>');
test love test