Dav
Dav

Reputation: 150

ActionScript Replace Between String?

I have a string named MESSAGE and it varies depending on what people say.

Is there a way I can make MESSAGE replace the contents of a string inside of a string?

For example, if MESSAGE is equal to "Hey guys [rainbow]look at my awesome rainbow text[/rainbow] isn't it cool?" then how do I only replace the "[rainbow]look at my awesome rainbow text[/rainbow]" part by getting rid of "[rainbow]" and "[/rainbow]" and replacing "look at my awesome rainbow text" with a string called RAINBOWTEXT?

The reason I need this is because I would like users in my Flash-based chat to be able to make rainbow text, but the method I use needs to do this client-side. I did have a PHP version that would submit the text into the database, but it would cut the message off due to the large amount of data. Each character got a <font color="#123456"> and a </font> on either side of it, so messages were super long.

If I can replace the inside part of what is specified by [rainbow] and [/rainbow], I can have each person's client replace the message once it gets it. In the database, it will have "Hey guys [rainbow]look at my awesome rainbow text[/rainbow] isn't it cool?" but in chat it will have "Hey guys look at my awesome rainbow text isn't it cool?" with the actual text rainbowfied.

Upvotes: 0

Views: 141

Answers (1)

net.uk.sweet
net.uk.sweet

Reputation: 12431

This sounds like a job for a regular expression and the replace() method. Have a dig around for expressions which will find and replace the contents of HTML tags and you should be able to adapt what you find to suit your requirements. Examples for JavaScript should also be valid as ActionScript and JavaScript both adhere to the ECMAScript standard for regular expressions.

Once you've found something, you can play around with it with this handy tool for testing ActionScript 3.0 regular expressions.

Upvotes: 1

Related Questions