简体   繁体   English

BBCode标签的RegExp javascript

[英]RegExp for BBCode tags javascript

I have this RegExp, and i dont know what's wrong with it 我有这个RegExp,但我不知道它怎么了

tag = new RegExp('(\\['+tag+'=("|'|)(.*?)\1\\])((?:.|\\r?\\n)*?)\\[/'+tag+']','g');

The bbcode tags can have double quotation marks, single quotation marks or no quotation marks. bbcode标记可以有双引号,单引号或无引号。

[tag="teste"]123[/tag]
[tag='teste']123[/tag]
[tag=teste]123[/tag]

Desired output in captures: teste and 123 捕获中所需的输出: teste123

To match the optional quotation marks, it should be ("|'|) , (["|\\']*) or ("|\\'?) ? 要匹配可选引号,应为("|'|)(["|\\']*)("|\\'?)

Whats wrong with the string 字符串怎么了

First, let's correct the syntax in your string 首先,让我们更正字符串中的语法

  • You need to define the var tag 您需要定义var tag

     tag = 'tag'; result = new RegExp( <...> ); 
  • You have unballanced quotes in '("|'|) <...> ' , that needs to be escaped as ("|\\'|) 您在'("|'|) <...> '没有多余的引号,需要将其转义为("|\\'|)

  • Also, escape \\1 as \\\\1 此外,将\\1 \\\\1\\\\1

so now we have the expression '(\\\\['+tag+'=("|\\'|)(.*?)\\\\1\\\\])((?:.|\\\\r?\\\\n)*?)\\\\[/'+tag+']' with the value: 所以现在我们有了表达式'(\\\\['+tag+'=("|\\'|)(.*?)\\\\1\\\\])((?:.|\\\\r?\\\\n)*?)\\\\[/'+tag+']' ,值:

(\[tag=("|'|)(.*?)\1\])((?:.|\r?\n)*?)\[/tag]

What's wrong with the RegEx 正则表达式有什么问题

Only one thing really, in ("|\\'|)(.*?)\\\\1 you're using \\1 to match the same quotation mark as the one used as opening. However, the 1 refers to the first capturing group (the first parenthesis from left to right), but ("|'|) is actually the second set of parenthesis, the second group. 确实只有一件事,在("|\\'|)(.*?)\\\\1您使用\\1来匹配与用作开头的引号相同的引号。但是,1表示第一个捕获组(第一个括号从左到右),但是("|'|)实际上是第二个括号集,第二个组。 All you need to do is change it to \\2 . 您需要做的就是将其更改为\\2

(\[tag=("|'|)(.*?)\2\])((?:.|\r?\n)*?)\[/tag]

That's it! 而已!

Let's add some final suggestions 让我们添加一些最终建议

  • Instead of .*? 代替.*? I would use [^\\]]+ (any characters except "]") 我会使用[^\\]]+ (“]”以外的任何字符)
  • Use the i modifier (case-insensitive match, for "[tag]...[/TaG]") 使用i修饰符(不区分大小写的匹配,用于“ [tag] ... [/ TaG]”)
  • ("|'|) is the same as ("|'?) ("|'|)("|'?)
  • Instead of (?:.|\\r?\\n)*? 而不是(?:.|\\r?\\n)*? I would use [\\s\\S]*? 我会使用[\\s\\S]*? as @nhahtdh suggested 如@nhahtdh建议

Code: 码:

tag = 'tag';
result = new RegExp('(\\['+tag+'=("|\'?)([^\\]]+)\\2\\])([\\s\\S]*?)\\[/'+tag+']','gi');

Alternative: [EDIT: from info added in comments] 备选方案: [编辑:从注释中添加的信息]

result = new RegExp('\\['+tag+'(?:=("|\'?)([^\\]]+)\\1)?\\]([\\s\\S]*?)\\[/'+tag+']', 'gi');

As for your second question: Although both (["|\\']*) and ("|\\'?) will match, the latter is the correct way for what you're trying to match. 关于第二个问题:尽管(["|\\']*)("|\\'?)都将匹配,但是后者是您要匹配的正确方法。 The * looks for 0 to infinite repetitions, and the | *寻找0到无限重复, | is interpreted as literal in a character class. 在字符类中被解释为文字。 Instead, ("|\\'?) matches a single quote, a double quote, or none. 而是("|\\'?)匹配单引号,双引号或不匹配。

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

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