a/20049344/1636522
HTML
<input id="search" value="^()\w\d$" />
<button id="submit">Highlight!</button>
<button id="revert">Revert!</button>
<p id="sample">Sample string ^()\w\d$ id span "" <span style="">toto</span> contains regexp</p>
JavaScript
$('#submit').click(function () {
var replacePattern = new RegExp(
$('#search').val().replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'),
'gi');
$('#sample').children().addBack().not('.highlight').contents().filter(function() {
return this.nodeType === 3;
}).replaceWith(function(){
return this.data.replace(replacePattern, '<b class="highlight">$&</b>');
});
});
$('#revert').click(function(){
$(".highlight").contents().unwrap();
});