JavaScript RegExp - Quick Guide



JavaScript RegExp - Overview

A regular expression is an object that describes a pattern of characters.

The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.

Syntax

A regular expression could be defined with the RegExp () constructor, as follows −

var pattern = new RegExp(pattern, attributes);
or simply
var pattern = /pattern/attributes;

Here is the description of the parameters −

  • pattern − A string that specifies the pattern of the regular expression or another regular expression.

  • attributes − An optional string containing any of the "g", "i", and "m" attributes that specify global, case-insensitive, and multi-line matches, respectively.

Example

Following example shows usage of RegExp to check if a string is present in given text or not.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.test(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "pushing", "g" );
         
         var result = re.test(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

Output

Test 1 - returned value : true
Test 2 - returned value : false 

JavaScript RegExp - [...]

Description

[...] checks if any one character between the brackets is present in the searched string.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first";
         var pattern = /[abcde]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 			 
      </script>
   </body>
</html>

Output

Test 1 - returned value : null
Test 2 - returned value : e,c,d 

JavaScript RegExp - [^...]

Description

[^...] checks if any one character between the brackets is not present in the searched string.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first";
         var pattern = /[^abcde]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : f,i,r,s,t
Test 2 - returned value : s,o,n 

JavaScript RegExp - [0-9]

Description

[0-9] matches any decimal digit from 0 through 9.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first";
         var pattern = /[0-9]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "2nd";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : null
Test 2 - returned value : 2 

JavaScript RegExp - [a-z]

Description

[a-z] matches any character from lowercase a through lowercase z.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first";
         var pattern = /[a-z]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "Second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : f,i,r,s,t
Test 2 - returned value : e,c,o,n,d 

JavaScript RegExp - [A-Z]

Description

[A-Z] matches any character from uppercase A through uppercase Z.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first";
         var pattern = /[A-Z]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "Second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : null
Test 2 - returned value : S

JavaScript RegExp - [a-zA-Z]

Description

[a-zA-Z] matches any character from lowercase a through uppercase Z.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first1";
         var pattern = /[a-zA-Z]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "Second2";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : f,i,r,s,t
Test 2 - returned value : S,e,c,o,n,d

JavaScript RegExp - p+

Description

p+ matches any string containing one or more p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p+/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pp
Test 2 - returned value : null

JavaScript RegExp - p*

Description

p* matches any string containing zero or more p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p*/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : ,,,pp,
Test 2 - returned value : ,,,

JavaScript RegExp - p?

Description

p? matches any string containing zero or more p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p?/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : ,,,p,p,
Test 2 - returned value : ,,,

JavaScript RegExp - p{N}

Description

p{N} matches any string containing a sequence of N p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p{2}/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abcp";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pp
Test 2 - returned value : null

JavaScript RegExp - p{N1, N2}

Description

p{N1, N2} matches any string containing a sequence of N1 or N2 p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p{2,3}/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abcp";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pp
Test 2 - returned value : null

JavaScript RegExp - p{N,}

Description

p{N,} matches any string containing at least a sequence of N p's.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p{2,}/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abcp";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pp
Test 2 - returned value : null

JavaScript RegExp - p$

Description

p$ matches any string containing p at the end of it.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcpp";
         var pattern = /p$/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : p
Test 2 - returned value : null

JavaScript RegExp - ^p

Description

^p matches any string containing p at the start of it.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "pabc";
         var pattern = /^p/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : p
Test 2 - returned value : null

JavaScript RegExp - [^a-zA-Z]

Description

[^a-zA-Z] matches any string not containing any of the characters ranging from a through z and A through Z.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "1323pabc";
         var pattern = /[^a-zA-Z]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 1,3,2,3
Test 2 - returned value : null

JavaScript RegExp - p.p

Description

p.p matches any string containing p, followed by any character, in turn followed by another p.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "pap";
         var pattern = /p.p/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "pp";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pap
Test 2 - returned value : null

JavaScript RegExp - ^.{2}$

Description

^.{2}$ matches any string containing exactly two characters.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "pa";
         var pattern = /^.{2}$/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "pap";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : pa
Test 2 - returned value : null

JavaScript RegExp - <b>(.*)<\/b>

Description

<b>(.*)<\/b> matches any string enclosed within <b> and </b>.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "<b>ab</b>";
         var pattern = /<b>(.*)<\/b>/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "pap";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : ab
Test 2 - returned value : null

JavaScript RegExp - p(hp)*

Description

p(hp)* matches any string containing a p followed by zero or more instances of the sequence hp.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "phphp";
         var pattern = /p(hp)*/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "pap";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : phphp
Test 2 - returned value : p,p

JavaScript RegExp - Alphanumeric

Description

Alphanumeric matches itself.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab1";
         var pattern = /abc/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : null
Test 2 - returned value : abc

JavaScript RegExp - \0

Description

\0 matches the NUL character (\u0000).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\0c";
         var pattern = /\0/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \t

Description

\t matches the Tab character (\u0009).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\tc";
         var pattern = /\t/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \n

Description

\n matches the New line character (\u00A).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\nc";
         var pattern = /\n/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \v

Description

\v matches the Vertical Tab character (\u00B).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\vc";
         var pattern = /\v/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \f

Description

\f matches the formfeed character (\u00C).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\fc";
         var pattern = /\f/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \r

Description

\r matches the Carriage return character (\u00D).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\rc";
         var pattern = /\r/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \xnn

Description

\xnn matches the Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\x0Ac";
         var pattern = /\x0A/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - \uxxxx

Description

\uxxxx matches the Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
		 var str = "ab\u0009c";
         var pattern = /\u0009/g;

         var index = str.search(pattern);
         document.write("Test 1 - returned value : " +  index); 

         str = "abc";
         index = str.search(pattern);
         document.write("<br/>Test 2 - returned value : " +  index); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 2
Test 2 - returned value : -1

JavaScript RegExp - .

Description

. matches a single character.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abc";
         var pattern = /./g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,b,c
Test 2 - returned value : null

JavaScript RegExp - \s

Description

\s matches a whitespace character (space, tab, newline).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab c";
         var pattern = /\s/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value :  
Test 2 - returned value : null

JavaScript RegExp - \S

Description

\S matches a non-whitespace character.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab c";
         var pattern = /\S/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,b,c
Test 2 - returned value : null

JavaScript RegExp - \d

Description

\d matches a digit (0-9).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab123c";
         var pattern = /\d/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : 1,2,3
Test 2 - returned value : null

JavaScript RegExp - \D

Description

\D matches a non-digit.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab123c";
         var pattern = /\D/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,b,c
Test 2 - returned value : a,b,c

JavaScript RegExp - \w

Description

\w matches a word character (a-z, A-Z, 0-9, _).

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab123c_#@";
         var pattern = /\w/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc bcd";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,b,1,2,3,c,_
Test 2 - returned value : a,b,c,b,c,d

JavaScript RegExp - \W

Description

\w matches a non-word character.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "ab123c_#@";
         var pattern = /\W/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "abc bcd";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : #,@
Test 2 - returned value :

JavaScript RegExp - [aeiou]

Description

[aeiou] matches a single character in the given set.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first alphabet";
         var pattern = /[abcde]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 			 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,a,b,e
Test 2 - returned value : e,c,d

JavaScript RegExp - [^aeiou]

Description

[^aeiou] matches a single character outside the given set.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "first alphabet";
         var pattern = /[^abcde]/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "second";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 			 
      </script>
   </body>
</html>

Output

Test 1 - returned value : f,i,r,s,t, ,l,p,h,t
Test 2 - returned value : s,o,n

JavaScript RegExp - (foo|bar|baz)

Description

(foo|bar|baz) matches any of the alternatives specified.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "foo abc";
         var pattern = /(foo|bar|baz)/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         str = "bar baz";
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 			 
      </script>
   </body>
</html>

Output

Test 1 - returned value : foo
Test 2 - returned value : bar,baz

JavaScript RegExp - i

Description

i perform case-insensitive matching.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcABC";
         var pattern = /[aeiou]/ig;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         pattern = /[aeiou]/g;
         result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : a,A
Test 2 - returned value : a

JavaScript RegExp - g

Description

g performs a global match that is, find all matches rather than stopping after the first match.

Example

Following example shows usage of RegExp expression.

<html>
   <head>
      <title>JavaScript RegExp</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "abcabcabc";
         var pattern = /abc/g;

         var result = str.match(pattern);
         document.write("Test 1 - returned value : " +  result); 

         pattern = /abc/;
		 result = str.match(pattern);
         document.write("<br/>Test 2 - returned value : " +  result); 	 		 
      </script>
   </body>
</html>

Output

Test 1 - returned value : abc,abc,abc
Test 2 - returned value : abc

JavaScript RegExp - constructor

Description

It returns a reference to the array function that created the instance's prototype.

Syntax

Its syntax is as follows −

RegExp.constructor

Return Value

Returns the function that created this object's instance.

Example

Following example shows usage of RegExp expression.

<html>   
   <head>
      <title>JavaScript RegExp constructor Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var re = new RegExp( "string" );
         document.write("re.constructor is:" + re.constructor); 
      </script>      
   </body>
</html>

Output

re.constructor is: function RegExp() { [native code] } 

JavaScript RegExp - global

Description

global is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs global matching, i.e., whether it was created with the "g" attribute.

Syntax

Its syntax is as follows −

RegExpObject.global

Return Value

Returns "TRUE" if the "g" modifier is set, "FALSE" otherwise.

Example

<html>   
   <head>
      <title>JavaScript RegExp global Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var re = new RegExp( "string" );
         
         if ( re.global ) {
            document.write("Test1 - Global property is set"); 
         } else {
            document.write("Test1 - Global property is not set"); 
         }
         re = new RegExp( "string", "g" );
         
         if ( re.global ) {
            document.write("<br />Test2 - Global property is set"); 
         } else {
            document.write("<br />Test2 - Global property is not set"); 
         }
      </script>   
   </body>
</html>

Output

Test1 - Global property is not set
Test2 - Global property is set

JavaScript RegExp - ignoreCase

Description

ignoreCase is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs case-insensitive matching, i.e., whether it was created with the "i" attribute.

Syntax

Its syntax is as follows −

RegExpObject.ignoreCase

Return Value

Returns "TRUE" if the "i" modifier is set, "FALSE" otherwise.

Example

<html>   
   <head>
      <title>JavaScript RegExp ignoreCase Property</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var re = new RegExp( "string" );
        
         if ( re.ignoreCase ) {
            document.write("Test1-ignoreCase property is set"); 
         } else {
            document.write("Test1-ignoreCase property is not set"); 
         }
         re = new RegExp( "string", "i" );
        
         if ( re.ignoreCase ) {
            document.write("<br/>Test2-ignoreCase property is set"); 
         } else {
            document.write("<br/>Test2-ignoreCase property is not set"); 
         }
      </script>      
   </body>
</html>

Output

Test1 - ignoreCase property is not set
Test2 - ignoreCase property is set 

JavaScript RegExp - lastIndex

Description

lastIndex a read/write property of RegExp objects. For regular expressions with the "g" attribute set, it contains an integer that specifies the character position immediately following the last match found by the RegExp.exec() and RegExp.test() methods. These methods use this property as the starting point for the next search they conduct.

This property allows you to call those methods repeatedly, to loop through all matches in a string and works only if the "g" modifier is set.

This property is read/write, so you can set it at any time to specify where in the target string, the next search should begin. exec() and test() automatically reset the lastIndex to 0 when they fail to find a match (or another match).

Syntax

Its syntax is as follows −

RegExpObject.lastIndex

Return Value

Returns an integer that specifies the character position immediately following the last match.

Example

<html>
   <head>
      <title>JavaScript RegExp lastIndex Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         re.test(str);
         document.write("Test 1 - Current Index: " +  re.lastIndex); 
         
         re.test(str);
         document.write("<br />Test 2 - Current Index: " + re.lastIndex); 
      </script>      
   </body>
</html>

Output

Test 1 - Current Index: 10
Test 2 - Current Index: 35 

JavaScript RegExp - multiline

Description

multiline is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs multiline matching, i.e., whether it was created with the "m" attribute.

Syntax

Its syntax is as follows −

RegExpObject.multiline

Return Value

Returns "TRUE" if the "m" modifier is set, "FALSE" otherwise.

Example

<html>   
   <head>
      <title>JavaScript RegExp multiline Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var re = new RegExp( "string" );
         
         if ( re.multiline ) {
            document.write("Test1-multiline property is set"); 
         } else {
            document.write("Test1-multiline property is not set"); 
         }
         re = new RegExp( "string", "m" );
         
         if ( re.multiline ) {
            document.write("<br/>Test2-multiline property is set"); 
         } else {
            document.write("<br/>Test2-multiline property is not set"); 
         }
      </script>      
   </body>
</html>

Output

Test1 - multiline property is not set
Test2 - multiline property is set

JavaScript RegExp - source

Description

source is a read-only string property of RegExp objects. It contains the text of the RegExp pattern. This text does not include the delimiting slashes used in regular-expression literals, and it does not include the "g", "i", and "m" attributes.

Syntax

Its syntax is as follows −

RegExpObject.source

Return Value

Returns the text used for pattern matching.

Example

<html>   
   <head>
      <title>JavaScript RegExp source Property</title>
   </head>
   
   <body>      
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         re.test(str);
         document.write("The regular expression is : " +  re.source); 
      </script>      
   </body>
</html>

Output

The regular expression is : script 

JavaScript RegExp - exec

Description

The exec method searches string for text that matches regexp. If it finds a match, it returns an array of results; otherwise, it returns null.

Syntax

Its syntax is as follows −

RegExpObject.exec( string );

Parameter Details

string − The string to be searched

Return Value

Returns the matched text if a match is found, and null if not.

Example

<html>
   <head>
      <title>JavaScript RegExp exec Method</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.exec(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "pushing", "g" );
         
         var result = re.exec(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

Output

Test 1 - returned value : script
Test 2 - returned value : null 

JavaScript RegExp - test

Description

The test method searches string for text that matches regexp. If it finds a match, it returns true; otherwise, it returns false.

Syntax

Its syntax is as follows −

RegExpObject.test( string );

Parameter Details

string − The string to be searched

Return Value

Returns the matched text if a match is found, and null if not.

Example

<html>
   <head>
      <title>JavaScript RegExp test Method</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.test(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "pushing", "g" );
         
         var result = re.test(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

Output

Test 1 - returned value : true
Test 2 - returned value : false 

JavaScript RegExp - toSource

Description

The toSource method string represents the source code of the object. This method does not work with all the browsers.

This method does not work with all the browsers.

Syntax

Its syntax is as follows −

RegExpObject.toSource();

Return Value

Returns the string representing the source code of the object.

Example

Try the following example program.

<html>
   <head>
      <title>JavaScript RegExp toSource Method</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.toSource(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "/", "g" );
         
         var result = re.toSource(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

Output

Test 1 - returned value : /script/g
Test 2 - returned value : /\//g

JavaScript RegExp - toString

Description

The toString method returns a string representation of a regular expression in the form of a regular-expression literal.

Syntax

Its syntax is as follows −

RegExpObject.toString();

Return Value

Returns the string representation of a regular expression.

Example

<html>
   
   <head>
      <title>JavaScript RegExp toString Method</title>
   </head>
   
   <body>
      <script type = "text/javascript">
         var str = "Javascript is an interesting scripting language";
         var re = new RegExp( "script", "g" );
         
         var result = re.toString(str);
         document.write("Test 1 - returned value : " +  result); 
         
         re = new RegExp( "/", "g" );
         
         var result = re.toString(str);
         document.write("<br />Test 2 - returned value : " +  result); 
      </script>
   </body>
</html>

Output

Test 1 - returned value : /script/g
Test 2 - returned value : /\//g
Advertisements