0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I got so far. pastebin.com/raw.php?i=Ynw5kTzL It works, however only for one instance of rainbows. What I mean is I can go like "just [rainbow]testing[/rainbow] something" and it works, but if I do "just [rainbow]testing[/rainbow] something oh look [rainbow]another test[/rainbow] tehe" it will not rainbow the second instance. Know how to modify my script so it will?
Looks like you got some help in another question so I can stand down ;). You should accept this answer if it set you off in the right direction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.