Skip to content Skip to sidebar Skip to footer

Replace With Regexp Only Outside Tags In The String

I have a strings where some html tags could present, like this is a nice day for bowling bbbb how can I replace with RegExp all b symbols, for example, with :bl

Solution 1:

The regex itself to replace all bs with :blablabla: is not that hard:

.replace(/b/g, ":blablabla:")

It is a bit tricky to get the text nodes where we need to perform search and replace.

Here is a DOM-based example:

functionreplaceTextOutsideTags(input) {
  var doc = document.createDocumentFragment();
  var wrapper = document.createElement('myelt');
  wrapper.innerHTML = input;
  doc.appendChild( wrapper );
  returntextNodesUnder(doc);
}
functiontextNodesUnder(el){
  var n, walk=document.createTreeWalker(el,NodeFilter.SHOW_TEXT,null,false);
  while(n=walk.nextNode())
  {
       if (n.parentNode.nodeName.toLowerCase() === 'myelt')
      		n.nodeValue =  n.nodeValue.replace(/:\/(?!\/)/g, "smiley_here"); 
  }
  return el.firstChild.innerHTML;
} 

var s = 'not feeling well today :/ check out this link <a href="http://example.com">http://example.com</a>';
console.log(replaceTextOutsideTags(s));

Here, we only modify the text nodes that are direct children of the custom-created element named myelt.

Result:

not feeling well today smiley_here check outthis link <a href="http://example.com">http://example.com</a>

Solution 2:

var input = "this is a nice day for bowling <b>bbbb</b>";
var result = input.replace(/(^|>)([^<]*)(<|$)/g, function(_,a,b,c){
				return a
				+ b.replace(/b/g, ':blablabla:')
				+ c;
});
document.querySelector("pre").textContent = result;
<pre></pre>

You can do this:

var result = input.replace(/(^|>)([^<]*)(<|$)/g, function(_,a,b,c){
            return a
            + b.replace(/b/g, ':blablabla:') // you may do something else here
            + c;
});

Note that in most (no all but most) real complex use cases, it's much more convenient to manipulate a parsed DOM rather than just a string. If you're starting with a HTML page, you might use a library (some, like my one, accept regexes to do so).

Solution 3:

I think you can use a regex like this : (Just for a simple data not a nested one)

/<[^\/]*?b.*?<\/.*?>|(b)/ig

[Regex Demo]


If you wanna use a regex I can suggest you use below regex to remove all tags recursively until all tags removed:

/<[^\/][^<]*>[^<]*<\/.*?>/g

then use a replace for finding any b.

Post a Comment for "Replace With Regexp Only Outside Tags In The String"