After posting about the new ExternalInterface class in Flash 8, there was a comment suggesting the use of it to get Regular Expression functionality into Flash. Well, I thought that was a fine idea, and it turns out it’s pretty easy to implement.
I’ve already covered some of the uses of ExternalInterface in this other blog post. It focuses on sending values from an HTML page into the Flash movie. That’s only half of what makes ExternalInterface so cool. Another way to use it is to call a Javascript function in your HTML page from your Flash movie and have it return a value.
If you want to jump right in, here’s my RegEx in Flash example page.
For this example I added two methods to the String class: String.match
and String.replace
. I used the prototype object because it’s the quickest way to add it in. Here’s the code in Flash:
// add match to String
String.prototype.match = function(reg) {
var arr = ExternalInterface.call("reMatch", escape(this.toString()), escape(reg));
for (var i=0;i<arr.length;i++) {
arr[i] = unescape(arr[i]);
}
return arr;
}
// add replace to String
String.prototype.replace = function(reg, rep) {
return ExternalInterface.call("reReplace", escape(this.toString()), escape(reg), escape(rep));
}
As you can see, the code is very simple. When using ExternalInterface.call
, the first argument is the name of the function you want to call in your HTML document, and the rest are arguments you want to pass in to it. The Flash player takes those values, passes them to your Javascript function and sends the result back in real time.
In my HTML document, I added two functions to handle the RegExp calls and return the values:
// RegEx replace
function reReplace(str, reg, rep) {
var s = unescape(str);
var re = new RegExp(eval(unescape(reg)));
return s.replace(re, unescape(rep));
}
// RegEx match
function reMatch(str, reg) {
var s = unescape(str);
return s.match(eval(unescape(reg)));
}
// escape the values in the array because
// ExternalInterface doesn't escape some properly
function encodeArray(arr) {
for (var i=0; i<arr.length;i++) {
arr[i] = escape(arr[i]);
}
return arr;
}
There’s a few more lines added in that test for the existence of ExternalInterface by checking the ExternalInterface.available
variable. If that returns false, I redirect the user to an upgrade message inside the swf file.
For most people this would be plenty of support for RegExp, but if you wanted even more control, it should be fairly easy to write an entire RegExp class that uses ExternalInterface and some Javascript functions to get full Regular Expression support into Flash.
Here’s a link to the example page again:
RegExp in Flash 8 using ExternalInterface.
UPDATE: Just wanted to add a link to a little RegExp class I found via Google. Looks like a nice alternative if you don’t mind the 20k file size.
UPDATE (2-24-2005): Updated this code to fix a couple bugs with non-escaped chars. This all works except when trying to match spaces. It looks like the Flash player has issues encoding spaces when using ExternalInterface. If you try to match something like /s/g
, you’ll get an array of ‘null’s back. I’ve also recreated the original fla file and zipped it up for download.
UPDATE: I went ahead and added a few more lines to the code so all the characters are properly escaped. Every regex you throw at it should work now.