Search within TutorialsPark

Javascript Regular Expressions: String Methods


Explore all Regular Expression Methods of String Object.


Javascript Regular Expressions: String Object Methods

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.

Method Description
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.

Javascript Regular Expressions: match() Method

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.


Javascript Regular Expression: search() Method

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.

Syntax: JavaScript Regular Expression - search() Method.

Example and Format: JavaScript Regular Expression - search() Method.

Give it a TRY! » Note:The method returns the position of the firdt match in the string.


JavaScript Regular Expressions: replace() Method

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.

Syntax: JavaScript Regular Expressions: replace() method.

Example: JavaScript Regular Expression: replace() method

Give it a TRY! » Note: If the regex has the global options flag g , then all matching substrings are replaced.


JavaScript Regular Expressions: Method split().

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.

Syntax: JavaScript Regular Expression split() method.

Example: JavaScript Regular Expression split() Method.

Give it a TRY! » Note:If delimiter is not specifed the string is not split at all.