I want to do form validation at user side with JavaScript (jQuery is also used). The goal is to remove nested bbCode [quote]
tags deeper than level 2. Say, we have this text:
[quote=SoundMAX][quote=Laplundik][quote=SoundMAX]
blahblahblah[/quote]
blahblah
[/quote]
blah[/quote]
And get this:
[quote=SoundMAX][quote=Laplundik]
blahblah
[/quote]
blah[/quote]
My only idea is to .replace [quote]
with <div>
, then create DOM object and remove anything deeper than 2 with jQuery, and parse all backwards to bbCode. But that solution seems too complicated, are there more elegant one?
EDIT:
Thanks for nice solutions. Based on darioo's answer, I did this:
var text=$('#edit-privatemsgbody').val();
var tmp=[];
var level=0;
for (var i=0,l=text.length;i<l;i++){
if(text[i]=='['&&text[i+1]=='q') level++;
if(text[i-6]=='q'&&text[i-7]=='/'&&text[i-8]=='[') level--;
if(level<3) tmp.push(text[i]);
}
alert(tmp.join(''));
Which works just fine.
But idealmachine's solution was like a flash. I didn't know about replace callback function parameters before, now that is handy! I'll settle with it.