首页 > 解决方案 > 涉及特殊字符的 Google 应用脚本搜索未终止的括号错误

问题描述

我的程序有以下行,我用另一个字符串替换部分字符串。

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]

我意识到它与正则表达式有关。但我自己无法解决。帮助将不胜感激。

苏吉思

标签: regexgoogle-apps-script

解决方案


推荐阅读