Skip to content Skip to sidebar Skip to footer

Regex Start And End Matching

So I'm having a little regex trouble, I have an expression that matches the starting with and the ending with separately. The problem occurs when I try to match the starting with a

Solution 1:

The problem is that you're not matching what's in between the opening and closing tags; the expression expects either an opening or closing tag to be the only contents of your string.

To match whatever is between the opening and closing tag you need something like this:

/\[gaiarch(?:=([^\]]+))?\](.*?)\[\/gaiarch\]/ig

For this expression to work you can use RegExp.exec():

var re = /\[gaiarch(?:=([^\]]+))?\](.*?)\[\/gaiarch\]/ig;
while ((match = re.exec(str)) !== null) {
    console.log(match[1]) // "slider"console.log(match[2]) // "[img url=...]"
}

Post a Comment for "Regex Start And End Matching"