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
Expression | Description |
[….] | 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
Expression | Description |
^a | It 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
Expression | Description |
\d | It is used indicate a digit. |
\D | It is used to indicate that it is not a digit. |
\w | It is used to indicate a word character. |
\W | It is used to indicate that it is not a word character. |
\s | It is used to indicate a whitespace character. |
\S | It 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
Expression | Description |
\f | It is used for form feed. |
\n | It is used for newline. |
\t | It is used for horizontal tab space. |
\r | It is used to enter key. |
\v | It is used for vertical tab. |
\\ | It is used to display backslashes \\. |
Modifiers
Expression | Description |
i | It is used to ignore case. |
m | It is used to multiline matching. |
g | It is used for global match and find all the matches. |
RegExp Properties
Properties | Description |
source | It is used to specify text of the pattern. |
constructor | It is used to provide a reference to RegExp function of the particular object. |
lastIndex | It is used to specify index to start the next match. |
multiLine | It is used to specify when ‘m’ modifier is set. |
global | It is used to specify when ‘g’ modifier is set. |
ignoreCase | It is used to specify when ‘i’ modifier is set. |
RegExp Methods
Methods | Description |
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.