假设我有一个列表['TEST CLIENT 1', 'test client 2']
。
通过输入文本(我将其存储在searchPattern
变量中),我必须突出显示单词(如果单词出现在数组的每个字符串中)。
如果我搜索客户端(或客户端),列表必须变为
['TEST <b> CLIENT </b> 1', 'test <b> client </b> 2']
item.replace(searchPattern, `<b>${searchPattern}</b>`)
searchPattern='test' res=[['TEST CLIENT 1', '<b>test</b> client 2']
searchPattern='TEST' res=[['<b>TEST</b> CLIENT 1', 'test client 2']
item.replace(new RegExp(searchPattern, 'gi'), `<b>${searchPattern}</b>`)}
searchPattern='test' res=[['<b>test</b> CLIENT 1', '<b>test</b> client 2']
searchPattern='TEST' res=[['<b>TEST</b> CLIENT 1', '<b>TEST</b> client 2']
item.replace(new RegExp(`(\\s|\\b)(${searchPattern})(\\s|\\b)`, 'gi'), `$1<b>$2</b>$3`)
searchPattern='test' res=['<b>TEST</b> CLIENT 1', '<b>test</b> client 2']
searchPattern='TEST' res=['<b>TEST</b> CLIENT 1', '<b>test</b> client 2']
但是
searchPattern='lie' || 'LIE' res=['TEST CLIENT 1', 'test client 2']
有可能吗?