const string1 = "The following %%variable%% has %%been%% signed"
const string2 = "The following jdfkdfk dfv dfv has been signed"
const r = new RegExp('^' +
string1
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
.replace(/%%.+?%%/g, '.+') +
'$')
console.log("Resulting pattern: " + r)
console.log(r.test(string2))
console.log(r.test('asd'))
1条答案
按热度按时间b4wnujal1#
您可以将string1转换为regex,它可以用于匹配其他字符串。要执行此操作,请执行以下操作:
1.转义正则表达式符号,
1.将所有与
%%.+?%%
匹配的内容替换为.+
,1.添加
^
和$
以防止部分匹配。字符串