Javascript Regex: How to bold specific words with regex?

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

    var needle = 'cows';
    
    var regexp = new RegExp('(^|\s)('+needle+')(\s|$)', 'ig');
    
    var old_string = 'cows at www.cows.com, milk some COWs';
    
    var new_string = old_string.replace(regexp, '$1$2$3');
    

提交回复
热议问题