简体   繁体   English

什么是带有标题:[name]的正则表达式匹配?

[英]What is regex match with caption : [name]?

How to match the regex to the format "caption: [name]" ? 如何将正则表达式与“标题:[名称]”格式匹配? I have used 我用过

^ ([a-zA-Z0-9]) + (. *?) + \: + (. *?) + $

but failed. 但失败了。

In your pattern you use spaces which have meaning. 在您的模式中,您使用具有意义的空格。 If you would remove all the spaces, your pattern would match and look like the following having 3 capturing groups: 如果要删除所有空格,则您的模式将匹配并看起来像下面的3个捕获组:

^([a-zA-Z0-9])+(.*?)+\:+(.*?)+$

About the pattern 关于图案

When you repeat the group using a quantifier (in this case + ), the group contains the value of the last iteration so you could move the quantifier inside the group. 当您使用量词(在本例中为+ )重复组时,该组包含上一次迭代的值,因此您可以将量词移到组内。

The first group matches until caption . 第一组匹配直到caption You could omit (.*?)+ as : is the next char and will be matched by \\:+ . 您可以省略(.*?)+因为:是下一个字符,并将与\\:+匹配。

That part itself can be written as : because you don't have to escape the colon and you want to match a single : . 该部分本身可以写成:因为您不必逃避冒号而您想要匹配一个:

The second group is not taking the opening and closing square brackets into account. 第二组没有考虑方括号的开头和结尾。 This could be matched using (\\[.*?\\]) or more efficient using a negated character class (\\[[^]]+\\]) 可以使用(\\[.*?\\])进行匹配,或者使用否定的字符类 (\\[[^]]+\\])更有效的匹配

If you want to keep the capturing groups, you pattern might look like: 如果要保留捕获组,则模式可能如下所示:

^([a-zA-Z0-9]+): (\[[^\]]+\])$

Regex demo 正则表达式演示

In your current regex you have lots of problems 在当前的正则表达式中,您有很多问题

  • ^ - This means match space after start of string whereas your input string doesn't have any ^ -这表示字符串开头后的匹配空间,而您输入的字符串中没有任何空格
  • ([a-zA-Z0-9])+ - This means match group one or more time, ie this tries to match repetitive characters ([a-zA-Z0-9])+ -这意味着匹配组一次或多次,即尝试匹配重复的字符
  • (. *?) - This means match any one character followed by zero or more space characters (. *?) -这表示匹配任意一个字符,后跟零个或多个空格字符
  • + - This means one or more space characters + -这表示一个或多个空格字符

You can use 您可以使用

/^[a-zA-Z0-9]+\s*:\s*\[[^\]]+\]$/

在此处输入图片说明

 let str = "caption: [name]" let reg = /^[a-zA-Z0-9]+\\s*:\\s*\\[[^\\]]+\\]$/ console.log(reg.test(str)) 

I'm guessing that you wish to design an expression similar to: 我猜您希望设计类似以下内容的表达式:

^([^:]+)\s*:\s*(\[[^\]]+\])$

and capture caption and [name] . 并捕获caption[name]


The expression is explained on the top right panel of this demo if you wish to explore/simplify/modify it. 如果您想探索/简化/修改该表达式,请在此演示的右上方面板中进行解释。

 const regex = /^([^:]+)\\s*:\\s*(\\[[^\\]]+\\])$/gm; const str = `caption: [name] other_captions: [other_names]`; let m; while ((m = regex.exec(str)) !== null) { // This is necessary to avoid infinite loops with zero-width matches if (m.index === regex.lastIndex) { regex.lastIndex++; } // The result can be accessed through the `m`-variable. m.forEach((match, groupIndex) => { console.log(`Found match, group ${groupIndex}: ${match}`); }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM