regex - 涉及特殊字符的 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]
我意识到它与正则表达式有关。但我自己无法解决。帮助将不胜感激。
苏吉思
解决方案
推荐阅读
- c# - EDI 解串器以不同的格式工作
- html - React - 单击时添加或删除类
- javascript - 如何单击一个点并将其坐标保存在 Leaflet 中?
- azure - Azure 数据工厂 - 休息复制活动附加标头不起作用
- google-cloud-platform - Terraform GCP - 参数在 windows-startup-script-ps1 中传递
- c# - 如何在 ScheduledTask 继承中实现依赖关系 (Telerik Sitefinity)
- kotlin - Mockito 如何测试在方法内创建的实例是否正在调用方法
- javascript - Javascript 代码在 JsFiddle 上工作,但在我的主机上不工作
- ruby-on-rails - 使用 Rails 6.1 应用程序在 Circle CI 上获得“未找到示例”
- python - TD Amertirade API 价格历史 - json 字符串返回