[英]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
捕获中所需的输出: teste
和123
To match the optional quotation marks, it should be ("|'|)
, (["|\\']*)
or ("|\\'?)
? 要匹配可选引号,应为("|'|)
, (["|\\']*)
或("|\\'?)
?
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 ("|\\'|)
您在'("|'|) <...> '
没有多余的引号,需要将其转义为("|\\'|)
\\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]
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! 而已!
.*?
代替.*?
I would use [^\\]]+
(any characters except "]") 我会使用[^\\]]+
(“]”以外的任何字符) ("|'|)
is the same as ("|'?)
("|'|)
与("|'?)
(?:.|\\r?\\n)*?
而不是(?:.|\\r?\\n)*?
I would use [\\s\\S]*?
我会使用[\\s\\S]*?
as @nhahtdh suggested 如@nhahtdh建议 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.