Javascript Regex: How to bold specific words with regex?

前端 未结 4 1666
礼貌的吻别
礼貌的吻别 2020-12-10 05:56

Given a needle and a haystack... I want to put bold tags around the needle. So what regex expression would I use with replace()? I want SPACE to be the delimeter and I want

4条回答
  •  情歌与酒
    2020-12-10 06:15

    For those who don't want SPACE as the delimiter, simply don't use \s.

    function updateHaystack(input, needle) 
    {
     return input.replace(new RegExp('(^|)(' + needle + ')(|$)','ig'), '$1$2$3');
    }
    

    Worked for me.

提交回复
热议问题