JavaScript Regular Expressions

JavaScript Regular Expressions

JavaScript regular expressions are strings which are used to describe search patterns. It’s a recipe for matching a string with another. A regular expression is used for string search or replace operations.

JavaScript Regular Expressions

When you want to search a string within a text, we can define search patterns using wild card characters to describe what we are searching. A search pattern can be of single character or a more complex pattern of more than one characters using special characters. You can use JavaScript regular expressions to perform all type of text search and text replace operations.

Syntax

/pattern/modifiers

Example

/exp/i

In above JavaScript regular expression, “exp” is a pattern and “i” is a modifier which means it’s a case-insensitive text search.

String methods

There are two methods in JavaScript which use regular expression : search() and replace().

search() method uses to search string using regular expression and returns position of string. replace() method uses regular expression to search and replace string and return modified string

search() Method

Below example uses a regular expression with search() method to search string “palace”.

<!DOCTYPE html>
<html>
<body>

search() function search for the given string and returns its position
<p id="text"></p>

<script>

var str = "This is a palace";
var pos = str.search(/palace/i);
document.getElementById('text').innerHTML = pos;
</script>

</body>
</html>

Download the code Run the code

string() method can also used without regular expression.

<!DOCTYPE html>
<html>
<body>

search() function search for the given string and returns its position
<p id="text"></p>

<script>

var str = "This is a palace";
var pos = str.search("palace");
document.getElementById('text').innerHTML = pos;
</script>

</body>
</html>

Download the code Run the code

replace() method

To search and replace string, use replace() method. Like search() method, replace() method is also used either with regular expression or without regular expression.

<!DOCTYPE html>
<html>
<body>

replace() function search for the given string and replace string and returns new replaced string.
<p id="text"></p>

<script>

var str = "This is a palace";
var pos = str.replace(/palace/i, 'mahal');
document.getElementById('text').innerHTML = pos;
</script>

</body>
</html>

Download the code Run the code

<!DOCTYPE html>
<html>
<body>

replace() function search for the given string and replace string and returns new replaced string.
<p id="text"></p>

<script>

var str = "This is a palace";
var pos = str.replace("palace", 'mahal');
document.getElementById('text').innerHTML = pos;
</script>

</body>
</html>

Download the code Run the code

Regular expressions are very powerful and use them for more complex searches.

Regular Expression Modifiers

Modifiers are used for case insensitive search and for multiple searches(global)

i		Used for case insensitive search
g		Used for multiple searches(all the occurrences of search string)
m		Used for multiple lines search

Regular Expression Patterns

Brackets are special characters in regular expressions and used to find range of characters.

[abc]		Find any characters within bracket
[0-9]		Find any numeric character given within brackets
(a|b)		Find alternative characters separated by |

Special meta characters

Metacharacters	Description
-------------------------------------------
\d			Represents Digit from 0 to 9
\s			Represents while spaces
\b			Represents beginning of line
\uxxxx			Represents Unicode characters

Quantifiers used to define quantities.

Metacharacters	Description
-------------------------------------------
+			Represents one or more characters
*			Represents zero or more characters
?			Represents zero or one characters

JavaScript RegExp Object

RegExp object is a regular expression object which has predefined properties and methods.

RegExp test() Method

test() method uses regular expression to search for a string and returns true if match is found otherwise it returns false.

Example

<!DOCTYPE html>
<html>
<body>

test() method returns true or false based on match.
<p id="text"></p>

<script>

var reg = /palace/;
var test = reg.test("This is a palace");
document.getElementById('text').innerHTML = test;
</script>

</body>
</html>

Download the code Run the code

Above test() method returns true as “palace” is exist in string.

It is not necessary to use a variable to assign a regular expression. You can directly use regular expression with test() method

<!DOCTYPE html>
<html>
<body>

test() method returns true or false based on match.
<p id="text"></p>

<script>
var test = /palace/.test("This is a palace");
document.getElementById('text').innerHTML = test;
</script>

</body>
</html>

Download the code Run the code

exec() method

exec() method is used to search for a string specified by pattern and returns found text. If no any text is found based on pattern than it returns null.

<!DOCTYPE html>
<html>
<body>

exec() method returns text matched with given pattern.
<p id="text"></p>

<script>
var test = /palace/.exec("This is a palace");
document.getElementById('text').innerHTML = test;
</script>

</body>
</html>

Download the code Run the code

Here is another tutorial on JavaScript regular expressions.

Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .