javascript - Regex to wrap all words in span, excluding img elements -


i trying find regex, in order wrap words of text in spans. using following code:

$this.html($this.text().replace(         /([^\s]*\s)/g, "<span>$1</span>")); 

where referred div element. however, img , elements, inside div, disappear. tried change regex into:

$this.html($this.text().replace(         /([^(\s | <a.*/a> | <img.*/img>)]*\s)/g, "<span>$1</span>")); 

but didn't fix problem.

can me?

replace non-whitespace (\s) there 1 or more (+) function, taking match parameter.

var str = 'hello dear world <img src="https://placeholdit.imgix.net/~text?txtsize=60&txt=1&w=100&h=100"/> hello dear world <img src="https://placeholdit.imgix.net/~text?txtsize=60&txt=1&w=100&h=100"/>';    function replacethings(str) {    var pre = "<span style='background-color:yellow'>";    var modstring = str      .replace(/\s+/ig, function(word) {        return pre + word + "</span>"      })    .split(pre + "<img").join("<img")    .split("/></span>").join("/>")    .replace(/<img{1}.*?\/>/ig, function (img){      return img.split(pre).join("").split("</span>").join("")    })      return modstring;  }  console.log( replacethings(str) );    document.body.innerhtml = replacethings(str)


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -