用于提取基于点概念的变量的动态Regex
Dynamic Regex for extracting dot notion based variables
我将使用什么Regex来返回包含scope.
的所有字符串(用点表示法),但返回包括后面任意数量点的完整值。
例如,下面的代码不返回".string"
部分。
> "scope.object.string".match(/(scope[.]'w+)/gi)
< ["scope.object"]
下面的代码将返回"scope.object.object2"
,因为我明确添加了第二个[.]'w+
,它不是动态的。
> "scope.object.object2.string".match(/(scope[.]'w+[.]'w+)/gi)
< ["scope.object.object2"]
我该如何动态地执行此操作,以便从以下字符串中返回此值:
> "scope.object.string scope.object.object2.string scope.object.object2.object3.string".match(/newRegex/)
< ["scope.object.string", "scope.object.object2.string", "scope.object.object2.object3.string"]
如果您可以在同一调用中使用相同的regex从每个字符串中删除"scope."
部分,那就更好了:
> "scope.object.string scope.object.object2.string scope.object.object2.object3.string".match(/newRegex/)
< ["object.string", "object.object2.string", "object.object2.object3.string"]
scope[.](?:'w+[.])*'w+
你可以用这个。如果要删除scope.
,请使用
scope[.]((?:'w+[.])*'w+)
抓住第1组。参见演示。
https://regex101.com/r/pT4tM5/24
var re = /scope[.]((?:'w+[.])*'w+)/gm;
var str = 'scope.object.object2.string'nscope.object.object2';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
- 在Javascript中添加多个变量以使用RegEx函数
- 从javascript regex.exec分配多个变量
- 使用javascript中的捕获组从变量创建regex
- 在RegEx(JavaScript)中正确转义变量
- 用于提取基于点概念的变量的动态Regex
- 使用带有.match Regex表达式的变量的JavaScript
- Backbone.js路由中的变量:需要regex
- 具有动态变量的Regex
- Regex JavaScript字符串并将结果存储到变量中
- Regex将一些变量转换为js
- Javascript:测试regex并赋值给变量,如果它在一行中匹配
- 如何将RegEx反向引用作为变量传递
- 如何在JQuery中使用regex (field $)向字符串变量添加整数
- Regex只接受变量名
- RegEx:从URL提取GET变量
- 将regex $1和$2保存为javascript变量
- 用字符串javascript regex匹配存储在变量或数组中的值
- 使用regex解释不同类型的变量,并使用jquery进行查找和替换
- PHP Regex从JSON变量中获取内容
- 使用包含Regex的变量