jQuery find/replace without changing original text

后端 未结 5 837
感动是毒
感动是毒 2020-12-09 14:06

Is there a way in jQuery to find a string of text in jQuery and not replace it with something else but wrap that text with an element so when the script is finished it spits

5条回答
  •  余生分开走
    2020-12-09 14:16

    This is simple. Just do a jQuery .replace() function.

    Here is a jsFiddle

    http://jsfiddle.net/HZVs8/

    The only issue with this method is that if you have any other words with the string "id" in them, it will convert those too. If the words are like "Project id", with a space, and not "Project-id" or "ProjectId", this method can be used like this:

    $("body").html(function(i, x) {
    return x.replace(/( id)/g, "ID");
    });​
    

    Make sure there is a space before the first id or it will select EVERY word that had the letters "id" in them.

提交回复
热议问题