String.match(regular expression)
|
This executes a search for a match within a string, based on a regular expression.
|
var myString = "today is 12-12-2000";
var matches = myString.match(/d{4}/);
//returns array ["2000"]
|
RegExp.exec(string)
|
This executes a search for a match in its string parameter. Unlike String.match , the parameter entered should be a string, not a regular expression pattern.
|
var pattern = /d{4}/;
pattern.exec("today is 12-12-2000");
//returns array ["2000"]
|
String.replace(regular expression, replacement text)
|
This searches and replaces the regular expression portion (match) with the replaced text instead.
|
var phone = "(201) 123-4567";
var phoneFormatted = phone.replace(/[()-s]/g, "");
//returns 2011234567 (removed ( ) - and blank space)
|
String.split (string literal or regular expression)
|
This breaks up a string into an array of substrings, based on a regular expression or fixed string.
|
var oldstring = "1,2, 3, 4, 5";
var newstring = oldstring.split(/s*,s*/);
//returns the array ["1","2","3","4","5"]
|
String.search(regular expression)
|
This tests for a match in a string. It returns the index of the match, or -1 , if it's not found.
|
var myString = "today is 12-12-2000";
myString.search(/d{4}/);
//returns 15 - index of 2000
|
RegExp.test(string)
|
This tests whether the given string matches the Regexp, and returns true if it's matching, and false, if not.
|
var pattern = /d{4}/;
pattern.test("today is 12-12-2000");
//returns true
|