jQuery find/replace without changing original text

后端 未结 5 824
感动是毒
感动是毒 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:13

    You can just use .replace(), for example:

    var str = "Hello world to all people";
    str = str.replace(/(world to all)/g, "$1");
    

    You can give it a try here applied to say the html of an element:

    $("span").html(function(i, t) {
        return t.replace(/(world to all)/g, "$1");
    });
    

提交回复
热议问题