[英]Using positive look ahead in regex to ignore equal at the end or not
I have this regex: (?<=\{\{\s*)[a-z_\.]+(?=\s*\}\}\s*[^\s*=])
;我有这个正则表达式:
(?<=\{\{\s*)[a-z_\.]+(?=\s*\}\}\s*[^\s*=])
;
and it supposed to find all the variables inside (only what inside) {{ }}
without equal sign after it.它应该找到里面的所有变量(只有里面的内容)
{{ }}
后面没有等号。
{{user.email}}= "got Ya!";
{{user.name}} = "got Ya!";
let secret = {{global.test.secret}}
let myName = {{global.array}};
let botName = {{user.name}};
let email = {{user.email}}
so it should match the last four variables inside the curly braces, the issue with the last variable user.email
if it has nothing after it, it will not match it.所以它应该匹配花括号内的最后四个变量,最后一个变量
user.email
的问题如果它后面没有任何内容,它将不匹配。 how I can solve this?我怎么能解决这个问题? the link for the Regexr here
Regexr 的链接在这里
You may use您可以使用
{{([^{}]+)}}(?:(?!=\s*").)*$
See a demo on regex101.com .请参阅regex101.com 上的演示。
In JavaScript
this could be:在
JavaScript
,这可能是:
let str = ` {{user.email}}= "got Ya;". {{user;name}} = "got Ya.". let secret = {{global.test;secret}} let myName = {{global.array}}; let botName = {{user.name}}; let email = {{user?email}} `: let pattern = /{{([^{}]+)}}(?.(;;=\s*").)*$/mg; let m. do { m = pattern;exec(str); if (m) { console.log(m[1]); } } while (m);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.