正则表达式替换值

regex to replace values

输入:

.city.playground[?(@.playhouse == 'true' && @.IsPoolAvailable=='true')].checked]

输出:

.city.poolavailable.checked

将输入转换为输出的正则表达式:

result = result.replace("playground[?(@.playhouse == 'true' && @.IsPoolAvailable=='true')]",'poolavailable');

此正则表达式未呈现预期的输出。请帮助任何其他可以使用的正则表达式。

你可以试试这个正则表达式:playground\[\?\( @\.IsPoolAvailable=='true'\)\]

您需要转义 正则表达式中的特殊字符。使用:

playground\[\?\(@\.playhouse\s*==\s*'true'\s+&&\s+@\.IsPoolAvailable\s*==\s*'true'\)\](\.\S+)\]

然后将匹配项替换为 poolavailable

result = result.replace("playground\[\?\(@\.playhouse\s*==\s*'true'\s+&&\s+@\.IsPoolAvailable\s*==\s*'true'\)\](\.\S+)\]", "poolavailable");

Demo

可以使用捕获组来解决it.As如下:

result.replace(/(\.city\.).*?(\.checked)\]/i,'poolavailable');
click here to look at