1

I have this RegExp, and i dont know what's wrong with it

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

The bbcode tags can have double quotation marks, single quotation marks or no quotation marks.

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

Desired output in captures: teste and 123

To match the optional quotation marks, it should be ("|'|), (["|\']*) or ("|\'?)?

3
  • 1
    Just some syntax errors: new RegExp('(\\['+tag+'=(["\']|)(.*?)\\1\\])([\\s\\S]*?)\\[/'+tag+']','g');. I also change ((?:.|\\r?\\n)*?) to ([\s\S]*?), since it seems to be your intention Commented Sep 4, 2015 at 3:02
  • if you don't know what's wrong with it, then how do you know it's wrong? Commented Sep 4, 2015 at 3:02
  • @Eduardo I found it difficult to read, so I expanded based on the info you provided in comments / answer Commented Sep 7, 2015 at 8:37

1 Answer 1

1

Whats wrong with the string

First, let's correct the syntax in your string

  • You need to define the var tag

    tag = 'tag';
    result = new RegExp( <...>  );
    
  • You have unballanced quotes in '("|'|) <...> ', that needs to be escaped as ("|\'|)

  • Also, escape \1 as \\1

so now we have the expression '(\\['+tag+'=("|\'|)(.*?)\\1\\])((?:.|\\r?\\n)*?)\\[/'+tag+']' with the value:

(\[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. All you need to do is change it to \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]")
  • ("|'|) is the same as ("|'?)
  • Instead of (?:.|\r?\n)*? I would use [\s\S]*? as @nhahtdh suggested

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 | is interpreted as literal in a character class. Instead, ("|\'?) matches a single quote, a double quote, or none.

Sign up to request clarification or add additional context in comments.

4 Comments

What was the way i wanted was the following code: new RegExp('\['+tag+'(?:=["\']([^\]]+)["\'])?\]([\\s\\S]*?)\[/'+tag+']', 'gi');
That code needs to be properly escaped. I assume you're now specifying: (a) you don't need the first group, and (b) the ='hello' part in the tag is optional but it should always have quotation marks. Code: result = new RegExp('\\['+tag+'(?:=(["\'])([^\\]]+)\\1)?\\]([\\s\\S]*?)\\[/'+tag+']','gi'); Live example
The best for me was the following: var re = new RegExp('\['+tag+'(?:=(["\']*)([^\]]+)\\1)?\]([\\s\\S]*?)\[/'+tag+']', 'gi');
I don't understand why you are repeating the quotes in ["\']*. Check the escapes on that expression. When using the RegExp constructor, every backslash should be put as `\\`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.