Pattern Matching
JavaScript has a RegExp object as well as pattern-matching methods in any String object. The regular expressions used are the same in both cases. Here are some of the String pattern-matching methods:
s.search(/pattern/);
Returns index of match if "pattern" found, or -1 if no match.
s.replace(/old/, "new");
s.replace(/old/g, "new");
Variables $1, $2, ... contain the
matches when g qualifier is used.
s.match(/pattern/);
Returns match as first element of array.
Other elements are parenthesized parts of pattern.
s.match(/pattern/g);
Returns array of all matches.
First "remembered match" when using parentheses in a
pattern is accessed by \1, second remembered
match is accessed by \2, and so on.
s.split("pattern");
Returns array of strings split from s by "pattern".
Here is a related and possibly useful array method:
a.join();
Turns the elements of a into strings and catenates them.
a.join("str");
Turns the elements of a into strings and catenates them,
with each pair separated by the string in "str".
strpos($some_string, $some_substring)
Returns the index of the substring in the string, if found;
otherwise returns false (which means that testing
for failure should be done with === since occurrence at the
beginning will return a 0 and this will be coerced to false
if == is used).
str_replace($old_substr, $new_substr, $str_to_be_searched)
Returns a copy of $str_to_be_searched in which each instance
of $old_substr has been replaced by $new_substr.
preg_match("/pattern/", $str)
Returns true if "pattern" is found in $str,
otherwise false. If the third parameter is used,
it is an array containing all the matches.
preg_match("/pattern/", $str, $array)
preg_split("/pattern/", $str)
Returns an array containing substrings of $str
split along boundaries matched by "pattern".
explode($separator, $string_to_be_separated)
Returns an array containing the substrings
between instances of $separator in
$string_to_be_separated.
implode($glue_string, $array_of_strings)
Either function (join is an alias for
implode) returns the string obtained
by inserting the contents of $glue_string
between each adjacent pair of string elements of
$array_of_strings.
join($glue_string, $array_of_strings)
index($some_string, $some_substring)
Returns the index of the substring in the string, if found;
otherwise returns -1.
s/old/new/)
Replaces the old pattern with the new (globally
if the g qualifier is used).
s/old/new/g)
/pattern/
is the same as any of
m/pattern/
m#pattern#
m(pattern)
m[pattern]
m{pattern}
m<pattern>
Returns TRUE if a match found, otherwise FALSE.
The first "remembered match" when using parentheses
is accessed by $1, the second by $2,
and so on; as well,
the parts of a string that preceded a match,
the part that matched, and the part that followed the
match, are rembered by the three built-in variables
$`, $&, and $', respectively.
split(" ", "one two three");
split(/pattern/, "any_string");
join($glue_string, @array_of_strings)
Returns a single string consisting of all the
strings in the array catenated with the "glue"
in between each two.
Here is an additional related, and occasionally useful
Perl function, the "transliteration" operator:
tr/old/new/
Returns string in which any character in "old"
has been replaced by the corresponding character
from "new".