我希望找到$ctrl.
之后的属性和我过go 执行此操作的正则表达式:/\$ctrl\.(\w+)(?:\.\w+)?/g
.
此正则表达式不够好,因为对于此输入:
const str =
'$ctrl.bla $ctrl.bar.foo $ctrl.bla{{index}}.foo $ctrl.1bla $ctrl.invalid.prop{{index}}';
它符合以下条件:
$ctrl.bla <--- good.
$ctrl.bar.foo. <-- not good. should find the `bar` because it's the word after $ctrl.
$ctrl.bla <--- goood
$ctrl.1bla. <-- invalid property syntax because it has number.
$ctrl.invalid.prop <-- should match the invalid word not matter that prop end with {{.
如何更改正则表达式以匹配有效的属性语法?
const regex = /\$ctrl\.(\w+)(?:\.\w+)?/g;
const str =
'$ctrl.bla $ctrl.bar.foo $ctrl.bla{{index}}.foo $ctrl.1bla $ctrl.invalid.prop{{index}}';
let match;
while ((match = regex.exec(str)) != null) {
console.log(match[0]);
}