Javascript Regex: How to bold specific words with regex?

前端 未结 4 1649
礼貌的吻别
礼貌的吻别 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:23

    findstring: /(^|\s)(cows)(\s|$)/ig
    newstring: '$1$2$3'
    

    The \b markers are for "word boundaries"; the /ig flags are for case-ignoring and global matching, respectively.

    The usage of the () captures and then $1/$2/$3 in the new string text is so that the capitalization and spacing of whatever was matched will be preserved.

提交回复
热议问题