SlideShare a Scribd company logo
REGULAR EXPRESSIONS
By
Dr.Smitha.P.S
Associate Professor
Velammal Engineering College
RegExp Object
• A regular 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
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Regular Expressions</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>
Example explained:
• /w3schools/i is a regular expression.
• w3schools is a pattern (to be used in a search).
• i is a modifier (modifies the search to be case-insensitive).
Modifiers
Modifier Description
g Perform a 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:
Brackets
Expression Description
[abc] Find any 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:
<!DOCTYPE html>
<html>
<body>
<p>Click the button 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
<!DOCTYPE html>
<html>
<body>
<p>Click the button 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
<!DOCTYPE html>
<html>
<body>
<p>Click the button 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,?
Metacharacters
Metacharacter Description
. Find a 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:
Quantifiers
Quantifier Description
n+ Matches any 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
RegExp Object Properties
Property Description
constructor Returns the function that created the RegExp object's
prototype
global Checks whether the "g" modifier is set
ignoreCase Checks whether the "i" modifier is set
lastIndex Specifies the index at which to start the next match
multiline Checks whether the "m" modifier is set
source Returns the text of the RegExp pattern
RegExp Object Methods
Method Description
compile() Deprecated in version 1.5. Compiles a regular expression
exec() Tests for a match in a string. Returns the first match
test() Tests for a match in a string. Returns true or false
toString() Returns the string value of the regular expression

More Related Content

What's hot (20)

PPTX
XML's validation - DTD
videde_group
 
PPT
2 dtd - validating xml documents
gauravashq
 
PPTX
Dom date and objects and event handling
smitha273566
 
PPTX
DTD
Kamal Acharya
 
PPTX
Introduction to XML
Abhra Basak
 
PPTX
Html (1)
smitha273566
 
PPTX
It8074 soa-unit i
smitha273566
 
PPT
Xml
Kunal Gaind
 
PPT
Xml Lecture Notes
Santhiya Grace
 
PPTX
XML, DTD & XSD Overview
Pradeep Rapolu
 
PPT
XML and DTD
Jussi Pohjolainen
 
PPTX
Php
Yoga Raja
 
PPTX
Unit iv xml dom
smitha273566
 
PPT
4 xml namespaces and xml schema
gauravashq
 
PPTX
Fergus Fahey - DRI/ARA(I) Training: Introduction to EAD - Introduction to XML
dri_ireland
 
PPT
XML Schema
yht4ever
 
PPT
XSD
Kunal Gaind
 
PPT
Introduction to XML
Vijay Mishra
 
XML's validation - DTD
videde_group
 
2 dtd - validating xml documents
gauravashq
 
Dom date and objects and event handling
smitha273566
 
Introduction to XML
Abhra Basak
 
Html (1)
smitha273566
 
It8074 soa-unit i
smitha273566
 
Xml Lecture Notes
Santhiya Grace
 
XML, DTD & XSD Overview
Pradeep Rapolu
 
XML and DTD
Jussi Pohjolainen
 
Unit iv xml dom
smitha273566
 
4 xml namespaces and xml schema
gauravashq
 
Fergus Fahey - DRI/ARA(I) Training: Introduction to EAD - Introduction to XML
dri_ireland
 
XML Schema
yht4ever
 
Introduction to XML
Vijay Mishra
 

Similar to Regular expression unit2 (20)

PPTX
Javascripting.pptx
Vinod Srivastava
 
PPTX
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
PPTX
10. session 10 loops and arrays
Phúc Đỗ
 
PPTX
Java Script
Kalidass Balasubramaniam
 
PPTX
Java: Regular Expression
Masudul Haque
 
PPSX
Php and MySQL
Tiji Thomas
 
PPTX
Regular expressions in Python
Sujith Kumar
 
PPTX
advancing in php programming part four.pptx
KisakyeDennis
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
DOC
14922 java script built (1)
dineshrana201992
 
PPTX
BITM3730 10-17.pptx
MattMarino13
 
PPTX
FYBSC IT Web Programming Unit III Javascript
Arti Parab Academics
 
PPTX
Java script functions
chauhankapil
 
PPT
Javascript built in String Functions
Avanitrambadiya
 
PPTX
07-PHP.pptx
GiyaShefin
 
PPTX
07-PHP.pptx
ShishirKantSingh1
 
PPTX
overview of php php basics datatypes arrays
yatakonakiran2
 
DOCX
WD programs descriptions.docx
anjani pavan kumar
 
Javascripting.pptx
Vinod Srivastava
 
String handling and arrays by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
UNIT II (7).pptx
DrDhivyaaCRAssistant
 
10. session 10 loops and arrays
Phúc Đỗ
 
Java: Regular Expression
Masudul Haque
 
Php and MySQL
Tiji Thomas
 
Regular expressions in Python
Sujith Kumar
 
advancing in php programming part four.pptx
KisakyeDennis
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
14922 java script built (1)
dineshrana201992
 
BITM3730 10-17.pptx
MattMarino13
 
FYBSC IT Web Programming Unit III Javascript
Arti Parab Academics
 
Java script functions
chauhankapil
 
Javascript built in String Functions
Avanitrambadiya
 
07-PHP.pptx
GiyaShefin
 
07-PHP.pptx
ShishirKantSingh1
 
overview of php php basics datatypes arrays
yatakonakiran2
 
WD programs descriptions.docx
anjani pavan kumar
 
Ad

Recently uploaded (20)

PPTX
Basics of Electrical Engineering and electronics .pptx
PrabhuNarayan6
 
PDF
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PDF
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
PDF
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
PPTX
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PPTX
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
PDF
Digital water marking system project report
Kamal Acharya
 
PDF
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
PDF
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
PDF
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
PPTX
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
PPTX
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
PPTX
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
PPTX
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
PDF
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Basics of Electrical Engineering and electronics .pptx
PrabhuNarayan6
 
NTPC PATRATU Summer internship report.pdf
hemant03701
 
PROGRAMMING REQUESTS/RESPONSES WITH GREATFREE IN THE CLOUD ENVIRONMENT
samueljackson3773
 
Tesia Dobrydnia - An Avid Hiker And Backpacker
Tesia Dobrydnia
 
Numerical-Solutions-of-Ordinary-Differential-Equations.pptx
SAMUKTHAARM
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Biosensors, BioDevices, Biomediccal.pptx
AsimovRiyaz
 
Digital water marking system project report
Kamal Acharya
 
William Stallings - Foundations of Modern Networking_ SDN, NFV, QoE, IoT, and...
lavanya896395
 
REINFORCEMENT LEARNING IN DECISION MAKING SEMINAR REPORT
anushaashraf20
 
Clustering Algorithms - Kmeans,Min ALgorithm
Sharmila Chidaravalli
 
OCS353 DATA SCIENCE FUNDAMENTALS- Unit 1 Introduction to Data Science
A R SIVANESH M.E., (Ph.D)
 
Introduction to Internal Combustion Engines - Types, Working and Camparison.pptx
UtkarshPatil98
 
Fundamentals of Quantitative Design and Analysis.pptx
aliali240367
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
MODULE 03 - CLOUD COMPUTING AND SECURITY.pptx
Alvas Institute of Engineering and technology, Moodabidri
 
Unit_I Functional Units, Instruction Sets.pptx
logaprakash9
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
DATA BASE MANAGEMENT AND RELATIONAL DATA
gomathisankariv2
 
3rd International Conference on Machine Learning and IoT (MLIoT 2025)
ClaraZara1
 
Ad

Regular expression unit2

  • 2. RegExp Object • A regular 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 Regular Expressions</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/i is 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 Perform a 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] Find any 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 the button 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 the button 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 the button 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 . Find a 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+ Matches any 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 Property Description constructor Returns the function that created the RegExp object's prototype global Checks whether the "g" modifier is set ignoreCase Checks whether the "i" modifier is set lastIndex Specifies the index at which to start the next match multiline Checks whether the "m" modifier is set source Returns the text of the RegExp pattern
  • 13. RegExp Object Methods Method Description compile() Deprecated in version 1.5. Compiles a regular expression exec() Tests for a match in a string. Returns the first match test() Tests for a match in a string. Returns true or false toString() Returns the string value of the regular expression