RegExp Object
• Aregular expression is an object that describes a pattern of
characters.
• Regular expressions are used to perform pattern-matching and
"search-and-replace" functions on text.
Syntax
/pattern/modifiers;
Example
var patt = /w3schools/i
3.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript RegularExpressions</h2>
<p>Click the button to do a case-insensitive search for "w3schools" in a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "Visit W3Schools";
var patt = /w3schools/i;
var result = str.match(patt);
document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>
4.
Example explained:
• /w3schools/iis a regular expression.
• w3schools is a pattern (to be used in a search).
• i is a modifier (modifies the search to be case-insensitive).
5.
Modifiers
Modifier Description
g Performa global match (find all matches rather than stopping
after the first match)
i Perform case-insensitive matching
m Perform multiline matching
Modifiers are used to perform case-insensitive and global searches:
6.
Brackets
Expression Description
[abc] Findany character between the brackets
[^abc] Find any character NOT between the brackets
[0-9] Find any character between the brackets (any digit)
[^0-9] Find any character NOT between the brackets (any non-
digit)
(x|y) Find any of the alternatives specified
Brackets are used to find a range of characters:
7.
<!DOCTYPE html>
<html>
<body>
<p>Click thebutton to do a global, case-insensitive, multiline search for "is" at the beginning of each line in a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "nIs thnis hnis?";
var patt1 = /^is/gmi;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>
Description
The ^n quantifier matches any string with n at the beginning of it.
Output
Is,is,is
8.
<!DOCTYPE html>
<html>
<body>
<p>Click thebutton to do a global search for at least one "o" in a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "Hellooo World! Hello W3Schools!";
var patt1 = /o+/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>
Output
Ooo,o,o,oo
9.
<!DOCTYPE html>
<html>
<body>
<p>Click thebutton to do a global search for characters NOT inside the brackets [h] in a string.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var str = "Is this all there is?";
var patt1 = /[^h]/g;
var result = str.match(patt1);
document.getElementById("demo").innerHTML = result;
}
</script>
</body>
</html>
Output
I,s, ,t,i,s, ,a,l,l, ,t,e,r,e, ,i,s,?
10.
Metacharacters
Metacharacter Description
. Finda single character, except newline or line terminator
w Find a word character
W Find a non-word character
d Find a digit
D Find a non-digit character
s Find a whitespace character
S Find a non-whitespace character
b Find a match at the beginning/end of a word, beginning like this: bHI, end like this: HIb
B Find a match, but not at the beginning/end of a word
0 Find a NULL character
n Find a new line character
f Find a form feed character
r Find a carriage return character
t Find a tab character
v Find a vertical tab character
xxx Find the character specified by an octal number xxx
xdd Find the character specified by a hexadecimal number dd
udddd Find the Unicode character specified by a hexadecimal number dddd
Metacharacters are characters with a special meaning:
11.
Quantifiers
Quantifier Description
n+ Matchesany string that contains at least one n
n* Matches any string that contains zero or more occurrences
of n
n? Matches any string that contains zero or one occurrences
of n
n{X} Matches any string that contains a sequence of X n's
n{X,Y} Matches any string that contains a sequence of X to Y n's
n{X,} Matches any string that contains a sequence of at least X n's
n$ Matches any string with n at the end of it
^n Matches any string with n at the beginning of it
?=n Matches any string that is followed by a specific string n
?!n Matches any string that is not followed by a specific string n
12.
RegExp Object Properties
PropertyDescription
constructor Returns the function that created the RegExp object's prototype
global Checks whether the "g" modifier is set
let pattern = /W3S/g;
let result = pattern.global;
Output
True
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
<html>
<body>
<h2>JavaScript Regular Expressions</h2>
<p>The lastIndex property specifies the index at which to start the next match:</p>
<p id="demo"></p>
<script>
let text = "The rain in Spain stays mainly in the plain";
let pattern = /ain/g;
let result = "";
while (pattern.test(text)==true) {
result += "Found at position " + pattern.lastIndex + "<br>";
}
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
Output
JavaScript Regular
Expressions
Found at position
8
Found at
position 17
Found at
position 28
Found at
position 43
13.
RegExp Object Methods
MethodDescription
compile() Deprecated in version 1.5. Compiles a regular expression
exec() Tests for a match in a string. Returns the first match
let text = "The best things in life are free";
let result = /e/.exec(text);
Ouput
e
test() Tests for a match in a string. Returns true or false
let text = "The best things in life are free";
let pattern = /e/;
let result = pattern.test(text);
Output
True
toString() Returns the string value of the regular expression
let pattern = new RegExp("Hello World", "g");
let text = pattern.toString();
Output
/Hello World/g