codescracker


javascript

JavaScript RegExp



« Previous Tutorial Next Tutorial »


A regular expression (RegExp) is an object that helps you to validate the pattern of characters.

JavaScript RegExp Object Quantifiers

The following table describes the list of quantifiers of the RegExp object in JavaScript.

Quantifier Description
n+ specifies a string that contains at least one n
n* specifies a string that contains zero or more occurrences of n
n? specifies a string that contains zero or one occurrence of n
n{a} specifies a string that contains n, a number of times
n{a, b} specifies a string that contains n, a to b number of times
n{a,} specifies a string that contains n, a or more number of times
n$ specifies a string that ends with n
^n specifies a string that begins with n
?=n specifies a string that is followed by a specific string n
?!n specifies a string that is not followed by a specific string n

JavaScript RegExp Object Properties

The table given below lists the properties of the RegExp object in JavaScript.

Property Description
global refers that the g modifier is set
ignoreCase refers that the i modifier is set
lastIndex refers the index to start the next match
multiline refers that the m modifier is set
source refers the text of the RegExp pattern

JavaScript RegExp Object Methods

Here is the table lists the methods of the RegExp object in JavaScript.

Method Description
compile() compiles the RegExp object
exec() tests for a match in a string and returns a result array
test() tests for a match in a string and returns true or false

JavaScript RegExp Object Example

Here is an example of RegExp object in JavaScript.

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript RegExp Object Example</title>
	<script>
	function regexp_fun1()
	{
		text1 = document.getElementById("regexp_para1").innerHTML; 
		document.getElementById("regexp_demo1").innerHTML = /s/.test(text1);
	}
	function regexp_fun2()
	{
		text2 = document.getElementById("regexp_para2").innerHTML; 
		document.getElementById("regexp_demo2").innerHTML = /s/.test(text2);
	}
</script>
</head>
<body>

<p>Search for "s" in the below paragraph.</p>
<p id="regexp_para1">This is codescracker</p>
<button onclick="regexp_fun1()">Search</button>
<p id="regexp_demo1"></p>
<hr/>
<p>Search for "s" in the below paragraph.</p>
<p id="regexp_para2">Hello World!</p>
<button onclick="regexp_fun2()">Search</button>
<p id="regexp_demo2"></p>

</body>
</html>

Here is the sample output produced by the above JavaScript RegExp object example program. This is the initial output:

javascript regexp object

Now click on both the Search button one by one to find the result as output as shown in the following snapshot.

javascript regexp object example

Here is the live demo output produced by the above RegExp object example program using JavaScript.

Search for "s" in the below paragraph.

This is codescracker


Search for "s" in the below paragraph.

Hello World!


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test