JavaScript regex - How to wrap matches with tag?

前端 未结 4 1197
走了就别回头了
走了就别回头了 2020-12-02 00:35

I have a string in JavaScript in which I\'d like to find all matches of a given phrase and wrap them with a tag. I haven\'t been able to find the right regex method here to

4条回答
  •  攒了一身酷
    2020-12-02 01:04

    You can do straight-up replace by using a replace function:

    str.replace(/cat/ig, function replace(match) { 
        return '' + match + ''; 
    });
    

提交回复
热议问题