Regular expressions are a standard syntax for searching for patterns in
strings. There syntax permits searching for complex patterns, there for
they are verry usefull for validating user inputs(like e-mail's, phone
numbers etc.). This article how ever will focus on the simpler use
cases: determining how many times a simple pattern repeats itself,
performing multiple replace actions in a string.
Lets take as an example a slot machine. Let's say you have 5 possible
fruits and 5 positions. You can represent these with number in an array
like so: [1, 1, 1, 1, 1], this would mean that all 5 positions have
fruit1 in them. Every time the user pulls the handle each position
changes randomly. Now lets say that you want to stop position from
changing if user got 2 or more of a kind (for example [1, 2, 1, 3 ,5] in
witch case you would want to stop position 0, and 2). You can do this 2
ways. Way nr.1 would be to loop trhough the array and use indexOf or
other self written methods, how ever i fail to see how could this be
easy. Way nr.2 would be using regular expressions like so:
[as]
var fruitArray:Array = [1, 2, 1, 3, 5];
var regExp:RegExp;
var tmpStr:String = fruitArray.toString();
for (i = 1; i <= 5 ; i++)
{
regExp = new RegExp(String(i), 'g');
trace(String(i), ' repeats ',tempStr.match(regExp).length , 'times')
}
[/as]
The above example will trace how many times each number from 1 to 5
repeats. Using the above code we can decide witch number repeats the
most times (in our case 1), then loop trough our position objects and
tell them to stop spining if the number is 1. Note than in this line:
"regExp = new RegExp(String(i), 'g');" String(i) is the pattern we are
searching for, 'g' is a RegExp flag (it means global) if we do not use
this flag we would always get 1 in our trace as searching would stop
when the first occurrence of the pattern is found (just like indexOf
returns the position of the first place where patter, or element is
found).
Another simple application of regular expressions is replacing multiple
occurrences of a pattern within a string. for example let's say we
have:h,e,l,l,o, ,w,o,r,l,d , and we want to remove all comas, the
simplest way to do this is replacing every "," with "". If we where to
use the replace as such:
[as]
var a:String='h,e,l,l,o, ,w,o,r,l,d';
trace(a.replace(',',''));
[/as]
The result would be:
he,l,l,o, ,w,o,r,l,d
But if we use replace in combination with regular expression like this:
[as]
var a:String='h,e,l,l,o, ,w,o,r,l,d';
var r:RegExp=new RegExp(',','g');
trace(a.replace(r,''));
[/as]
The result will be: hello world
Plase note that this can be achieved like this as well:
[as]
var a:String='h,e,l,l,o, ,w,o,r,l,d';
trace(a.replace(/,/g,''));
[/as]
Also note that the syntax used in the last example is best only if your
have constant search terms. As in this example we where always searching
for "," but in our first example are search term was variable, there
for the long version ( "regExp = new RegExp(String(i), 'g');" ) is
easier to use.
This article was motivated by the fact that I couldn't find any article
describing how exactly can regexp be used with variable search terms(the
examples i found didn't work for me).