Java Reference
In-Depth Information
RegExp Methods
Once you have created a regular expression object, you can use the
test()
to see if a
string (passed to the method as a parameter) matches the regular expression pattern. It re-
turns
true
if the pattern is in the string, and
false
if it isn't:
var pattern = /.*ing/;
<< undefined
pattern.test("joke"); //testing if the string ends in 'ing'
<< false
pattern.test("joking");
<< true
The
exec()
method works the same as the
test()
method, but instead of returning
true
or
false
, it returns an array containing the first match found or
null
if there aren't
any matches:
pattern.exec("joke"); //testing if the string ends in 'ing'
null
pattern.exec("joking");
["joking"]
Basic Regular Expressions
At the most basic level, a regular expression will just be a string of characters, so the fol-
lowing will match the string '
java
':
/java/
