Sunday, July 10, 2011

ActionScript Regular Expression Tester/Visualizer

Here is an improved version of my older post. Given that I already had the index of each occurrence in my hand, why not visualize it ? I chose the RichText spark component to show the matches.

I wanted to make the font style to bold and change the color to red of each match. I first thought of looping through each occurrence and concatenating to produce the RichText. But it wasn't necessary at all. The replace function of string took in a RegEx and all was easy. The replace function not only took in RegEx but also had the functionality to include the matching string in the replacement string as a parameter. That's right! The parameter is $&. See the code below. Oh and I had to handle the new lines too.