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)

PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
PDF
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
PDF
Regular expressions
ssuser8779cd
 
PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PPTX
Regular Expressions
Akhil Kaushik
 
PPT
Regular expressions
Raj Gupta
 
PPT
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
PPT
regular-expressions lecture 28-string regular expression
smallboss311
 
PDF
What is JavaScript Regex | Regular Expressions in JavaScript | Edureka
Edureka!
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PPTX
Regex lecture
Jun Shimizu
 
PPTX
Regular expressions
Ивелин Кирилов
 
KEY
Regular Expressions 101
Raj Rajandran
 
PPTX
Regular expressions
Thomas Langston
 
PDF
Regularexpressions
Raghu nath
 
PDF
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
PDF
Coffee 'n code: Regexes
Phil Ewels
 
PDF
Regular expressions
Raghu nath
 
PDF
Python (regular expression)
Chirag Shetty
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
Regular expressions
ssuser8779cd
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Regular Expressions
Akhil Kaushik
 
Regular expressions
Raj Gupta
 
Adv. python regular expression by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
 
regular-expressions lecture 28-string regular expression
smallboss311
 
What is JavaScript Regex | Regular Expressions in JavaScript | Edureka
Edureka!
 
3.2 javascript regex
Jalpesh Vasa
 
Regex lecture
Jun Shimizu
 
Regular expressions
Ивелин Кирилов
 
Regular Expressions 101
Raj Rajandran
 
Regular expressions
Thomas Langston
 
Regularexpressions
Raghu nath
 
2013 - Andrei Zmievski: Clínica Regex
PHP Conference Argentina
 
Coffee 'n code: Regexes
Phil Ewels
 
Regular expressions
Raghu nath
 
Python (regular expression)
Chirag Shetty
 
Ad

Recently uploaded (20)

PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PPTX
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
PPTX
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
PPTX
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
DOCX
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Day2 B2 Best.pptx
helenjenefa1
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Types of Bearing_Specifications_PPT.pptx
PranjulAgrahariAkash
 
artificial intelligence applications in Geomatics
NawrasShatnawi1
 
Product Development & DevelopmentLecture02.pptx
zeeshanwazir2
 
Thermal runway and thermal stability.pptx
godow93766
 
8th International Conference on Electrical Engineering (ELEN 2025)
elelijjournal653
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Day2 B2 Best.pptx
helenjenefa1
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
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