2
我想替換另一個字符串中的字符串。我的問題是,搜索字符串是一個變量,我需要爲此設置一個組,因爲我想重用原始匹配字符串。JS /正則表達式:如何在正則表達式中設置多個替換組?
const string = 'Any string with many words'
const label = 'any'
const re = new RegExp(label, 'gi')
string = string.replace(
re,
'>>>' + $1 + '<<<'
)
console.log(string)
// Expect: >>>Any<<< string with m>>>any<<< words
使用''>>> $&<<<'' –