Link to home
Create AccountLog in
Avatar of bobbit31
bobbit31Flag for United States of America

asked on

RegEx to implement BBCode style quoting in a forum post

hello experts,

I need a regular expression that will match something like the following:

------------------------------------------------------
[quote name='some name' date='some date']
This is what is being quoted.
[/quote]
------------------------------------------------------

this should also support nested quotes, such as:
------------------------------------------------------
[quote name='some name' date='some date']
   [quote name='other name' date='other date']
       This is a nested quote.
   [/quote]
   this is the original quote.
[/quote]
------------------------------------------------------

the quote doesn't necessarily have to be the first thing either... there can be text both before and after.

if possible, i'd like to make the name and date attributes optional (as in, they don't have to be there).

it's important that it matches both the opening and closing tag as i'm going to be replacing it with divs and if you leave out the [/quote] it will end up breaking the layout.

Thank you, your help is greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of Lolly-Ink
Lolly-Ink
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of bobbit31

ASKER

var r = /.*\[quote( name=('|").*('|"))?( date=('|").*('|"))?\].*\[\/quote\].*/i;

is close, however, the match data comes up wrong:

example:
[quote name='b' date='n']test1[quote name='c' date='m']test2[/quote]this is the original quote.[/quote]

$1 is:
name='b' date='n']test1[quote name='c' date='m'

Also, i need to know what is b/w the quote tags... so something like this?

var r = /.*\[quote( name=('|").*('|"))?( date=('|").*('|"))?\](.*)?\[\/quote\].*/i;

when i do that, $3 is:
b' date='n']test1[quote name='c' date='m'