Preserving case / capitalization with JavaScript replace method

后端 未结 2 390
感动是毒
感动是毒 2021-01-14 00:48

I\'m continuing work on a search term suggestion tool using Jquery UI. I am now working on displaying the results with the search term pattern in bold. I have implemented

2条回答
  •  滥情空心
    2021-01-14 01:14

    You can add your expression in a group by encapsulating them in parentheses

    var exp = new RegExp("(" + this.term + ")", "gi") ;
    var rep = item.label.replace( exp, "$1");
    

    You can the refere to that group using $1.

    See here for more details about backreferences.

提交回复
热议问题