我的程序有以下行,我将字符串的一部分替换为另一行。
for('condition'){
Searched_in_Text_Mod = Searched_in_Text_Mod.replace(new RegExp('\\b'+split_search_key+'\\b','g')," ");
}
只要没有特殊字符,代码就能正常运行。
如果split_search_key有任何特殊字符,则替换操作将终止,如下所示:(记录器输出和执行记录)
[18-06-16 13:22:14:832 IST] Sat Jun 16 13:22:14 GMT+05:30 2018
[18-06-16 13:22:15:609 IST] regex = /\bGrass .*& .*Plastic .* .* .*(Brown .*& .*Pink)\b/
[18-06-16 13:22:15:610 IST] Searched_in_Text = Meghalaya Grass & Plastic (Brown & Pink)
[18-06-16 13:22:15:610 IST] 11.0
[18-06-16 13:22:15:610 IST] Searched_in_Text_Mod = Meghalaya Grass & Plastic (Brown & Pink)
[18-06-16 13:22:15:611 IST] split_search_key = Grass
[18-06-16 13:22:15:611 IST] Searched_in_Text_Mod = Meghalaya & Plastic (Brown & Pink)
[18-06-16 13:22:15:612 IST] split_search_key = &
[18-06-16 13:22:15:612 IST] Searched_in_Text_Mod = Meghalaya & Plastic (Brown & Pink)
[18-06-16 13:22:15:613 IST] split_search_key = Plastic
[18-06-16 13:22:15:613 IST] Searched_in_Text_Mod = Meghalaya & (Brown & Pink)
[18-06-16 13:22:15:614 IST] split_search_key =
[18-06-16 13:22:15:614 IST] Searched_in_Text_Mod = Meghalaya & ( Brown & Pink )
[18-06-16 13:22:15:615 IST] split_search_key =
[18-06-16 13:22:15:615 IST] Searched_in_Text_Mod = Meghalaya & ( Brown & Pink )
[18-06-16 13:22:15:616 IST] split_search_key = (Brown
[18-06-16 13:22:15:615 IST] Logger.log([split_search_key = , []]) [0 seconds]
[18-06-16 13:22:15:616 IST] Logger.log([Searched_in_Text_Mod = Meghalaya & ( Brown & Pink ), []]) [0 seconds]
[18-06-16 13:22:15:616 IST] Logger.log([split_search_key = (Brown, []]) [0 seconds]
[18-06-16 13:22:15:668 IST] Execution failed: SyntaxError: Unterminated parenthetical . (line 49, file "Code") [0.786 seconds total runtime]
我意识到它与正则表达式有关。但我自己无法解决。非常感谢帮助。
萨吉斯