String and Regular Expression methods
The String object has four methods that take regular expressions as arguments. These are your workhorse methods that allow you to match, search, and replace a string using the flexibility of regular expressions:
String Methods Using Regular Expressions
|match( regular expression )||
Executes a search for a match within a string
based on a regular expression. It returns an array of information
or null if no match are found.
Note: Also updates the $1…$9 properties in the RegExp object.
|replace( regular expression, replacement text )||
Searches and replaces the regular expression
portion (match) with the replaced text instead.
Note: Also supports the replacement of regular expression with the specified RegExp $1…$9 properties.
|split ( string literal or regular expression )||Breaks up a string into an array of substrings based on a regular expression or fixed string.|
|search( regular expression )||Tests for a match in a string. It returns the index of the match, or -1 if not found. Does NOT support global searches (ie: "g" flag not supported).|
Here are a few examples:
var string1="Peter has 8 dollars and Jane has 15" parsestring1=string1.match(/\d+/g) //returns the array [8,15]
var string2="(304)434-5454" parsestring2=string2.replace(/[\(\)-]/g, "") //Returns "3044345454" (removes "(", ")", and "-")
var string3="1,2, 3, 4, 5" parsestring3=string3.split(/\s*,\s*/) //Returns the array ["1","2","3","4","5"]
Delving deeper, you can actually use the replace() method to modify- and not simply replace- a substring. This is accomplished by using the $1…$9 properties of the RegExp object. These properties are populated with the contents of the portions of the searched string that matched the portions of the search pattern contained within parentheses. The following example illustrates how to use the replace method to swap the order of first and last names and insert a comma and a space in between them:
The output of this code will be “Doe, Jane”. How this works is that the pattern in the first parentheses matches “Jane” and this string is placed in the RegExp.$1 property. The \s (space) character match is not saved to the RegExp object because it is not in parentheses. The pattern in the second set of parentheses matches “Doe” and is saved to the RegExp.$2 property. The String replace() method takes the Regular Expression object as its first argument and the replacement text as the second argument. The $2 and $1 in the replacement text are substitution variables that will substitute the contents of RegExp.$2 and RegExp.$1 in the result string.
You can also use replace() method to strip unwanted characters from a string before testing the string for validity or before saving the string to a database. It can be used to add formatting characters for the display of a string as well.
RegExp methods and properties
You just saw several regular expression related string methods; in most situations, they are all you need for your string manipulation needs. However, true to the versatility of regular expressions, the Regular Expression (RegExp) object itself also supports two methods that mimic the functions of their string counterparts, the difference being these two methods take strings as parameters, while with String functions, they take a RegExp instead. The following describes the methods and properties of the regular expression object.
|test(string)||Tests a string for pattern matches. This method returns a Boolean that indicates whether or not the specified pattern exists within the searched string. This is the most commonly used method for validation. It updates some of the properties of the parent RegExp object following a successful search.|
|exec(string)||Executes a search for a pattern within a string. If the pattern is not found, exec() returns a null value. If it finds one or more matches it returns an array of the match results. It also updates some of the properties of the parent RegExp object.|
Here is a simple example that uses test() to see if a regular expression matches against a certain string:
var pattern=/php/i pattern.test("PHP is your friend") //returns true
RegExp instance properties
Whenever you define an instance of the regular expression (whether using the literal or constructor syntax), additional properties are exposed to this instance which you can use:
n represents a number from 1 to 9
Stores the nine most recently memorized portions of a parenthesized match pattern. For example, if the pattern used by a regular expression for the last match was /(Hello)(\s+)(world)/ and the string being searched was “Hello world” the contents of RegExp.$2 would be all of the space characters between “Hello” and “world”.
|source||Stores a copy of the regular expression pattern.|
|global||Read-only Boolean property indicating whether the regular expression has a "g" flag.|
|ignoreCase||Read-only Boolean property indicating whether the regular expression has a "i" flag.|
|lastIndex||Stores the beginning character position of the last successful match found in the searched string. If no match was found, the lastIndex property is set to –1.|
This simple example shows how to determine whether a regular expression has the "g" flag added:
var pattern=/php/g alert(pattern.global) //alerts true
- Tutorial introduction (2 ways to define a regular expression)
- Categories of pattern matching characters
- String and Regular Expression methods
- Sample Usage