You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用正则匹配文本中所有符合display_加数字规则的字符串

JS正则匹配所有指定模式字符串的修改方案

原有代码存在的问题

  • 正则末尾的$锚定符限制仅匹配字符串结尾位置,无法匹配文本中间出现的目标模式
  • 未添加全局匹配标识g,String.prototype.match()默认仅返回第一个匹配结果
  • 传入字符串形式的正则规则存在隐式转义风险,更推荐使用正则字面量语法

修改后的实现代码

let text = "data.check && Obj[data.check]['display_119']) {  1549: displayName = obj[data.check]['display_12']";

var identifiers = text.match(/display_[0-9]+/g);
// 执行后identifiers值为 ["display_119", "display_12"],完全符合预期

内容的提问来源于stack exchange,提问作者xMayank

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 23:27:00