Preserving case / capitalization with JavaScript replace method

后端 未结 2 358
感动是毒
感动是毒 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:06

    You can use:

    var rep = item.label.replace(exp,
                                 "$&");
    

    When replacing a string, $& means "the whole match", so you don't have to repeat the search term (in some cases you don't know it). In other flavors, you may use $0 or \0.
    Also, remember to escape special characters in this.term.

提交回复
热议问题