Q:

AS3 BBcode or replacement-technique?

I'm making a website in AS3/PHP (databasecalls) and I want to load news on the homepage, fed from a mySQL database. No problem to get plain text with AMFPHP. But I'm looking for something different. I know how to load images/YouTube videos within AS3, so I want to spice up the news a little with that. Is there any way I can input something like
Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
[video]AAAAAAAAAAA[/video]
Donec commodo condimentum enim, vitae consectetur felis pharetra a.

it would make an instance of my YouTube API class right between my text and make two TextFields? There's no BBcode library for AS3, so my second guess was ... regex. Can't seem to wrap my head around that though, anyone ever wanted to try the same thing and succeeded?
Thanks in advance.

A:

This is a perfect job for a regular expression. 
var myPattern:RegExp = /\[video\]([a-zA-Z0-9_-]+)\[\/video\]/g;  

The brackets [ ] and the slash / are reserved characters, so they need to be escaped with a backslash. [a-zA-Z0-9_-] is the range of characters that can be in a valid youtube id. 
To use the regex you go like this:
var str:String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. \n[video]AAAAAAAAAAA[/video]\nDonec commodo condimentum enim, vitae consectetur felis pharetra a.";

var result:Object = myPattern.exec(str);
while (result != null) {
    trace( "id is: " + result[0] + " at index: " + result.index);
    result = myPattern.exec(str);
}

You can also compress the same code into fewer lines by putting the assingment as the while condition:
var result:Object;
while (result == myPattern.exec(str)) {
    trace( "id is: " + result[0] + " at index: " + result.index);
}

This is functionally equivalent, but can be a bit harder to grasp.
Now you have the index and the id, all that remains is to split up the text and insert the video!

