codescracker


javascript

JavaScript Regular Expression



« Previous Tutorial Next Tutorial »


A regular expression also known as regex or regexp, is a special text string which is used in describing a search pattern.

You can use regular expressions to search for patterns in string, replace text, and extract substrings.

JavaScript Regular Expression Example

Here is an example of regular expression to validate email address using JavaScript:

<!DOCTYPE HTML>
<HTML>
<HEAD>
	<TITLE>JavaScript Regular Expression Example</TITLE>
	<SCRIPT type="text/javascript">
	function validateUsingRegexp(this_form1)
	{
		var emailTxtVal = this_form1.email.value;
		var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
		var n1 = emailTxtVal.indexOf("@");
		var n2 = emailTxtVal.lastIndexOf(".");
		if(emailTxtVal=="")
		{
			alert("Please enter your email address.");
			this_form1.email.focus();
			return false;
		}
		if(emailTxtVal.match(illegalChars))
		{
			alert("Sorry! You have entered an invalid email address! Please try again.");
			this_form1.email.focus();
			return false;
		}
		if(n1<1 || n2-n1<2)
		{
			alert("Sorry! You have entered an invalid email address! Please try again.");
			this_form1.email.focus();
			return false;
		}
		return true;	
	}
	</SCRIPT>
</HEAD>
<BODY>

<FORM action="" onsubmit="return validateUsingRegexp(this)" method="Post">
	Username: <INPUT type="text" name="uname"/><BR/>
	E-mail: <INPUT type="text" name="email"/>*<BR/>
	<INPUT type="submit" value="submit"/>
</FORM>

</BODY>
</HTML>

Here are some sample outputs of the above regular expression example program using JavaScript. This is the initial output:

javascript regular expression

Now enter codescracker as username and codes/\cracker as email, and press on the submit button. Here is the output you will watch:

javascript regexp

Here is the live demo output produced by the above regular expression program using JavaScript:

Username:
E-mail: *

Here is another example also demonstrates how to use regular expression to search for string in the given string using JavaScript:

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript Regular Expression Example</title>
	<script>
	function regularExpressionFun2()
	{
		var str = "This is CodesCracker"; 
		var n = str.search("CodesCracker");
		document.getElementById("paragraph2").innerHTML = n;
	}
	</script>
</head>
<body>

<p>Search for "CodesCracker" in the string "This is CodesCracker"</p>
<button onclick="regularExpressionFun2()">Search</button>
<p id="paragraph2"></p>

</body>
</html>

Here is the sample output produced by the above JavaScript regular expression example program:

regular expression javascript

Now click on the Search button to find the output as shown in the following snapshot:

javascript regular expression example

Here is the live demo output of the above regular expression program in JavaScript:

Search for "CodesCracker" in the string "This is CodesCracker"

Let's take another example, also demonstrates regular expression in JavaScript using replace() method:

<!DOCTYPE html>
<html>
<head>
	<title>JavaScript Regular Expression Example</title>
	<script>
	function regularExpressionFun3()
	{
		var str = document.getElementById("paragraph3").innerHTML; 
		var txt = str.replace(/codescracker/i,"Regular Expression");
		document.getElementById("paragraph3").innerHTML = txt;
	}
	</script>
</head>
<body>

<p>Replace "codescracker" with "Regular Expression" in the below paragraph.</p>
<button onclick="regularExpressionFun3()">Replace</button>
<p id="paragraph3">This is codescracker</p>

</body>
</html>

Here is the output produced by the above JavaScript regular expression example:

javascript regular expression program

Here is the output produced after clicking on the Replace button:

regular expression in javascript

Here is the live demo output of the above regular expression example in JavaScript:

Replace "codescracker" with "Regular Expression" in the below paragraph.

This is codescracker


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test