我试图customer.name
从hello @customer.name
文本字符串的末尾捕获。
但是,我似乎无法从中@
脱颖而出。它给@customer.name
。
现在,我的正则表达式为:
@([0-9a-zA-Z.]+)$
使用.match
字符串的方法。结果将是null
与给定的正则表达式不匹配,否则它将是一个数组,其中:
@customer.name
您的情况,第一个元素是整个匹配的字符串customer.name
所需的字符串。看到这个片段。您的正则表达式看起来已经正确,您只需要从其中拉出捕获组,而不是整个匹配的字符串即可。
const str = "hello @customer.name"
const reg = /@([0-9a-zA-Z.]+)$/;
const match = str.match(reg)
console.log(match)
console.log("Matched string:", match[0]);
console.log("First capture group:", match[1]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句