Introduction:
In this article I am going to explain you about the RegExp Object it is a short form of Regular Expression. Which are very important because they provide us pattern-matching, search and replacing of text possible in JavaScript?
Syntax:
var pat=new RegExp(pattern,attributes);
or more simply like
var pat=/pattern/modifiers;
In the above syntax pattern specifies the expression which we are going to search for
And the attributes specify that if a search is going to be global or case-sensitive etc.
Brackets:
Brackets have a special meaning when used with Regular Expression they are used basically to specify range of characters.
Expression |
Description
|
[. . .] |
Find any character between the bracket. |
[^. . .] |
Find any character not between the bracket. |
[0 - 9] |
It matches any decimal digits from 0-9. |
[a – z] |
It matches any character from lowercase letter a to lowercase letter z. |
[A – Z] |
It matches any character from uppercase letter A to uppercase letter Z. |
[a –Z] |
It matches any character from lowercase letter a to uppercase letter Z. |
|
|
The above ranges shown are general we can create our own like [0-6] it will match any decimal between 0 and 6 likewise we can match any characters [a-d], matches all the characters between character a to z.
Metacharacters:
Metacharacters are simply alphabetical characters which have special meaning; they are proceeding with a backslash which give a special meaning to the combination.
Expression |
Description
|
. |
It is used to find a single character except newline or line terminator. |
\w |
It is used to find word character (a-z,A-Z,0-9 and Underscore). |
\W |
It is used to find a non-word character. |
\d |
It is used to find a digit from 0-9. |
\D |
It is used to find all non-digit character. |
\s |
It is used to find white space character (space,tab,newline) |
\S |
It is used to find non-whitespace character. |
\b |
It is used to find a match at the beginning of a word. |
\B |
It is used to find a match not at the beginning or end of a word. |
\0 |
It is used to find a NULL character. |
\n |
It is used to find a newline character. |
\r |
It is used to find a tab character. |
Quantifiers:
The frequency of bracketed characters and single characters can be denoted by a special character, each of the special character have some specific meaning.
Quantifiers |
Description
|
p+ |
It will match any string which contains minimum one p or more. |
p* |
It will match any string that contains zero or more occurrences of p. |
p? |
It will match any string that contains zero or one occurrences of p. |
p{N} |
It will match any string that contains a sequence of N p’s. |
p{x,y} |
It will match any string that contains the sequence of x to y p’s. |
p{x} |
It matches any string that contains a sequence of at least x p’s. |
p$ |
It matches any string with p at the end of it. |
^p |
It matches any string which is started at p. |
|
|
Example showing the use of RegExp:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is a first example of RegEx";
var pattern1 = /example/;
document.write(str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
output
example
RegExp Methods:
Method |
Description |
exec() |
It is used to execute a search for a string parameter passed to it. If no match found returns null. |
Test() |
It searches string for text that matches regExp. It returns true or false. |
toString() |
It returns the string representation of a specified object. |
toSource() |
It returns the object literal of the specified object and we can use this to create a new object |
Example of test method in RegExp:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var pattern1 = new RegExp("example");
document.write(pattern1.test("This is a example of test method"));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Example of exec method in RegExp:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var pattern1 = new RegExp("example");
document.write(pattern1.exec("This is a example of test method"));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
Example
Perform case-insensitive matching:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is a first Example of RegEx";
var pattern1 = /example/i;
document.write(str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
Display all matches with the characters specified in the brackets:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is a first Example of RegEx";
var pattern1 = /[a-f]/ig;
document.write(str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp ObjectExample showing the use of Metacharacters
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is a first example of RegEx";
var pattern1 = /ex.mple/;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp Object
Example to find a word in a given string:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is a first example of RegEx";
var pattern1 = /\w/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
This example will search for the non-word character:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "ab *&^ cd @! ef";
var pattern1 = /\W/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp Object
This example is used to search for digits:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "ab123cd456ef";
var pattern1 = /\d/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp Object
This example is to find out the non-digit characters:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "ab453!*fg45)(";
var pattern1 = /\D/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp Object
This example is to find out the white-space character :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is example to find whitespace character";
var pattern1 = /\s/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Output:
JavaScript RegExp Object
This example is about the use of b metachar to find a match at the beginning of a word:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Regexp Example in JavaScript</title>
<script type="text/javascript">
function regExpress() {
var str = "This is example to find whitespace character";
var pattern1 = /\bex/g;
document.write("Metachar example: " + str.match(pattern1));
}
</script>
</head>
<body onload="regExpress()">
</body>
</html>
Conclusion:
In this article I gave introduction to RegExp and what are the various uses in JavaScript through various examples.
Leave Comment