Javascript RegExp Object

The RegExp object is known as regular expression, it is used to specify the pattern of characters. Regular expressions are patterns used to match character combinations in strings. This is used for the pattern matching and also very useful for search and replace the text. In JavaScript, these are objects. This tutorial describes how to use the regular expression objects and its various methods.

Javascript RegExp Syntax

var pattern= new RegExp(patterns, attributes)
or
var  pattern = /patterns/attributes
  • pattern: It is used to specify the string pattern.
  • attributes: It is an optional string which contains global, multiline and ignore case.

Bracket

ExpressionDescription
[….]It is used to write the characters between these bracket.
[^…]It is used not write any character between the bracket.
[0-9]It is used to match a digit from 0 to 9
[a-z]It is used to match any lowercase letter from ‘a ‘to ‘z’
[A-Z]It is used to match any uppercase letter from ‘A’ to ‘Z’
[a-Z]It is used to match any lowercase letter ‘a’ to uppercase letter ‘Z’

Quantifiers

ExpressionDescription
^aIt is used to match the string with ‘a’ in beginning of the string.
a*It is used to match any string having zero’s or more a’s
a?It is used to match any string having one or more a’s
a{2,}It is used to match any string containing the sequence of 2 a’s.
a{2,3}It is used to match any string containing the sequence of 2 or 3 a’s
a+It is used to match string which contains at least 1 a
a$It is used to match the string ‘a’ at the end of the string
a{N}It is used to match a string which contains sequence of N a’s

Meta Characters

ExpressionDescription
\dIt is used indicate a digit.
\DIt is used to indicate that it is not a digit.
\wIt is used to indicate a word character.
\WIt is used to indicate that it is not a word character.
\sIt is used to indicate a whitespace character.
\SIt is used to indicate that it is not a whitespace character.
[^aeiou]It is used to find the any character except the letter inside the bracket.

Literal Characters

ExpressionDescription
\fIt is used for form feed.
\nIt is used for newline.
\tIt is used for horizontal tab space.
\rIt is used to enter key.
\vIt is used for vertical tab.
\\It is used to display backslashes \\.

Modifiers

ExpressionDescription
iIt is used to ignore case.
mIt is used to multiline matching.
gIt is used for global match and find all the matches.

RegExp Properties

PropertiesDescription
sourceIt is used to specify text of the pattern.
constructorIt is used to provide a reference to RegExp function of the particular object.
lastIndexIt is used to specify index to start the next match.
multiLineIt is used to specify when ‘m’ modifier is set.
globalIt is used to specify when ‘g’ modifier is set.
ignoreCaseIt is used to specify when ‘i’ modifier is set.

RegExp Methods

MethodsDescription
toSource()It is used to return the string using source code of the RegExp object
toString( )It specifies RegExp object into string.
test( )It is used to test the match in string by specifying true or false.
exec( )It is used to find a match of the specified value.

Example for RegExp Methods

<!DOCTYPE html>
<head>

</head>
<body>
<script>
	var a = "Sound is the vocabulary of nature";
	var b = new RegExp("e");
	var c = b.exec(a);
	var rp = b.test(a);
	document.write("The match of the string <b>e</b> in text <b>Sound is the vocabulary of nature</b> is: " +c);
	document.write("<br>Test the string: " +rp);
</script>
</body>
</html>
 
  • In the above program we have used RegExp Methods.
  • We have used two methods in the program i.e. exec and test.
  • We have set a variable “var a = “Sound is the vocabulary of nature”;.
  • var b = new RegExp(“e”); is used to search a string for the character “e”.
  • var c = b.exec(a); is used to find a match of the specified value.
  • var rp = b.test(a); is used to return true if it matches the string or else it return false.
  • “document.write” is used to display the exec and test values in the output.

Javascript RegExp Demo

  • Save the file as RegExp_methods.html in your system.
  • Just open the file in the browser, you will see the below picture in the browser. Note that the browser must support HTML specification.

When the execution process is completed successfully we will get the following output:

Javascript RegExp Demo

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*