[英]What should be the regex to match the start of a string
我有一个类似的字符串,我只想匹配每个{{xxxx}}
模式的第一个'{'
{ {abcd}} { {efg}} { {hij}}
{ {abcd}} { {efg}} { {hij}} {
我尝试使用/(\\s|^|.){/g
但此模式匹配
{ {abcd} } { {efg} } { {hij}}
有人能指导我正确的方向吗
您需要使用/(^|[^{]){/g
(将其匹配并捕获到组1的字符串开头或除{
之外的任何其他字符,然后匹配{
)并检查组1是否在每个匹配RegExp#exec
迭代。 然后,如果第1组匹配,则增加匹配索引:
var re = /(^|[^{]){/g; var str = "{{abcd}}{{efg}}{{hij}}\\n{{abcd}}{{efg}}{{hij}}{"; // 0 8 15 23 31 38 45 var m, indices = []; while ((m = re.exec(str)) !== null) { indices.push(m.index + (m[1] ? 1 : 0)); } console.log(indices);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.