Explore all Regular Expression Methods of String Object.
The String object's four methods can also be used while working with regular expressions.These methods are explained in table below.
Table: String Object methods for Regular Expressions.
|match()||To perform regular expression match on a string|
|search()||To search a match between the regular expression and the specified string.|
|replace()||Finds a match between regular expression and a string and then replace matched substring with the new substring.|
|split()||To split the substring into an array of strings by seperating the strings into substrings.|
The method match() is used to find or more regular-expressions matches, the behaviour of this method depends on whether the regexp has the flag option global "g".
Syntax: match() Method.
Example: match() Method.Give it a TRY! » Note:If the method does not return a null value, then a match must have been found.
The method search() looks for a substring matching regexp within string and if found, returns the position of the first character of the matching substring.
If no match is found then a -1 is returned. Also, the search() method does not perform global searches, it will ignore the g flag.
The method replace() is used to perform a search-and-replace operation on a string.
It searches the strings for one or more substrings that match the regular expression and then replaces them with the replacement string.
The method split() is used to break a string into an array of strings. It has two arguments: delimiter and limit
delimiter: To specify the string or regular expression at which the string splits.
limit: To specify the maximum length of the returned array.If not specified the entire string will be split.