SlideShare a Scribd company logo
2
Most read
4
Most read
10
Most read
toan @vnaking
front-end developer at ticketbox.vn
Regex
in javascript
Use
Regular expressions are used to perform pattern-
matching and "search-and-replace"
functions on text.
Basic
/pattern/flags
● two forward slashes to show the beginning and end of the pattern
● flags: (optional) indicate how to search (modifier).
Ex: /th/g (the g flag means "global" match - i.e. find all)
Syntax (Anchors)
^ Matches the beginning of input
^a : “alice” not “banana”
^: “a linenand break” => 2 match
$ Matches the end of input. a$ : “obama” not “name”
. Matches any single character except a newline a.b : “a0b”, “acb” not “ab”
b Word boundary bdogb : “dog”, “dog ” not “doggy”
B Not word boundary BdogB : “doggy”, “adogg” not “dog”
Syntax (Anchors)
s Whitespace (space, tab, newline,...) sc: matches “a cat” not “xcat”
S Not white space Sc: matches “cat” not “a cat”
d Digit d: matches “1” in “1abc”
D Not digit D: matches “b” in “123b”
w Word (alphanumeric character including _) w: matches “a” in “a%”
W Not word W: matches “%” in “a%”
Syntax (Quantifiers)
*
Matches the preceding character 0 or more
times
zo* : “z” and “zoo”
+
Matches the preceding character 1 or more
times
zo+ : “zoo” not “z”
? Matches the preceding character 0 or 1 time ca?r : only “car” and “cr”
{n} Matches exactly n times. o{2} : “fooood” not “bob”
{n,} Matches at least n times o{2,} : “fooood” not “bob”
{n,m} Matches the preceding character n to m times o{2,3} : “food”, “foood” not “fod”
Syntax (Group & Range)
| or (z|w)o : “zo” and “wo”
(...)
Matches subexpression and remembers the
match
(ab): “abc” -> 1st group match “ab”
(?:...)
Matches subexpression and do not remembers
the match (the match doesn’t in array)
(ab): “abc” -> No group
(?=...) Matches only if there is a following pattern win(?=7): “win” in “win7” not “win95”
(?!...) Matches only if there is not a following pattern win(?!=7): “win8” not “win7”
[...] Range
c[ma-c]: “cm”, “cb” not “cd”
c[A-E0-5]: “cB”, “c1” not c6
[^...] Not in range c[^ab]: “cc” not “ca”
Syntax (Modifier)
g global match (find all) /is/g : “this is” => two match
i Case-i-nse-nsitive /my/i : “My” and “my”
m Multiple line
/is/m : “this
is”
Replacement
$& insert the whole regex match "1a2b".replace(/d+/g, "($&)") => "(1)a(2)b"
$1,… $9
Insert the text matched by one of the
first 9 capturing groups.
"abc".replace(/(a)(b)(c)/g, "$3$2$1") => "cba"
$`
Insert the part of the subject string to
the left of the regex match
"abc".replace(/b/, "$`") => “aac”
$'
Insert the part of the subject string to
the right of the regex match
"abc".replace(/b/, "$'") => “acc”
● Creating
○ var re = new RegExp("pattern","flags"); or var re = /pattern/flags;
● Testing for matches:
○ /pattern/.test("this-is-a-string"): returns a Boolean telling you whether the string
contains a match of the pattern
● Executes a search:
○ /pattern/.exec("this-is-a-string"): returns an array
Creating a Regex
● Find all http in ticketbox’s url and change to https.
○ http://ticketbox.co.th -> https://ticketbox.co.th
○ http://www.ticketbox.vn -> https://www.ticketbox.vn
● find decremented event id by one for all ticketbox event url
○ https://ticketbox.vn/event/saigon-heat-vs-hitech-bangkok-city-34148 -> https:
//ticketbox.vn/event/saigon-heat-vs-hitech-bangkok-city-34149
● Find all date in format dd/mm/yyyy and convert to date
○ 17/11/2015 -> Tue, 17 Nov 2015
● Swap firstname, lastname in a list of name (split by commas)
○ Toan Nguyen, Vu Phan, Dao Le -> Nguyen Toan, Phan Vu, Le Dao
Practices
Practice
"http://ticketbox.vn/event/axwell-ingrosso-41450 http://ticketbox.co.th/saigon-heat-53982
https://ticketbox.co.th/event/fungjailoy-53838 http://TICKETBOX.vn/event/ingrosso-41451
https://ticketbox.vn/event/53982/ticket-booking http://acb.com/fungjailoy-53838".replace
(https?://ticketbox.(?:vn|co.th)/event/(?:d|w|-)+(d+)s, function(a, b) { return a.replace(b,
parseInt(b)+1)});
Output:
"http://ticketbox.vn/event/axwell-ingrosso-41451 http://ticketbox.co.th/saigon-heat-53982
https://ticketbox.co.th/event/fungjailoy-53839 http://TICKETBOX.vn/event/ingrosso-41452
https://ticketbox.vn/event/53982/ticket-booking http://acb.com/fungjailoy-53838"
Ref
http://www.regular-expressions.info/
http://eloquentjavascript.net/09_regexp.html
http://overapi.com/regex/
http://regexr.com/
http://www.cs.cf.ac.uk/Dave/Internet/NEWS/regexp.html
join us
https://ticketbox.vn/jobs

More Related Content

PDF
3.2 javascript regex
Jalpesh Vasa
 
PPT
Sql ppt
Anuja Lad
 
PPT
PL/SQL Introduction and Concepts
Bharat Kalia
 
PPTX
Code generation
Aparna Nayak
 
PPT
JavaScript Objects
Reem Alattas
 
PDF
PL/SQL TRIGGERS
Lakshman Basnet
 
PDF
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
PPT
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
3.2 javascript regex
Jalpesh Vasa
 
Sql ppt
Anuja Lad
 
PL/SQL Introduction and Concepts
Bharat Kalia
 
Code generation
Aparna Nayak
 
JavaScript Objects
Reem Alattas
 
PL/SQL TRIGGERS
Lakshman Basnet
 
JavaScript - Chapter 5 - Operators
WebStackAcademy
 
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 

What's hot (20)

PDF
Xml schema
Prabhakaran V M
 
PPTX
Loops in java script
Ravi Bhadauria
 
PPTX
Applets in java
Wani Zahoor
 
PPTX
PHP FUNCTIONS
Zeeshan Ahmed
 
PPTX
Demand paging
Trinity Dwarka
 
PDF
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PDF
Code optimization in compiler design
Kuppusamy P
 
PPTX
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
PPTX
Java package
CS_GDRCST
 
PPT
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
PPTX
Xml namespace
GayathriS578276
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Advanced SQL
Sabiha M
 
PPTX
Servlets
ZainabNoorGul
 
PPTX
Java exception handling
BHUVIJAYAVELU
 
ODP
Ms sql-server
Md.Mojibul Hoque
 
PPTX
Semi join
Alokeparna Choudhury
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Introduction to Javascript By Satyen
Satyen Pandya
 
Xml schema
Prabhakaran V M
 
Loops in java script
Ravi Bhadauria
 
Applets in java
Wani Zahoor
 
PHP FUNCTIONS
Zeeshan Ahmed
 
Demand paging
Trinity Dwarka
 
JavaScript - Chapter 8 - Objects
WebStackAcademy
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Code optimization in compiler design
Kuppusamy P
 
The role of the parser and Error recovery strategies ppt in compiler design
Sadia Akter
 
Java package
CS_GDRCST
 
Control Structures In Php 2
Digital Insights - Digital Marketing Agency
 
Xml namespace
GayathriS578276
 
Inheritance in java
Tech_MX
 
Advanced SQL
Sabiha M
 
Servlets
ZainabNoorGul
 
Java exception handling
BHUVIJAYAVELU
 
Ms sql-server
Md.Mojibul Hoque
 
Interface in java
PhD Research Scholar
 
Introduction to Javascript By Satyen
Satyen Pandya
 
Ad

Viewers also liked (6)

PPT
Regular Expressions
Satya Narayana
 
PDF
A3 sec -_regular_expressions
a3sec
 
PDF
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
PPT
Regular Expressions
Niek Schmoller
 
ODP
Regex Presentation
arnolambert
 
KEY
Regular Expressions 101
Raj Rajandran
 
Regular Expressions
Satya Narayana
 
A3 sec -_regular_expressions
a3sec
 
Regular Expressions: JavaScript And Beyond
Max Shirshin
 
Regular Expressions
Niek Schmoller
 
Regex Presentation
arnolambert
 
Regular Expressions 101
Raj Rajandran
 
Ad

Similar to Regular expression in javascript (20)

PPT
Introduction to Perl
Sway Wang
 
PDF
Regular Expressions in Javascript
Fen Slattery
 
PPT
regular-expressions lecture 28-string regular expression
smallboss311
 
PDF
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
PPTX
Regular expressions
Thomas Langston
 
PDF
Real life-coffeescript
David Furber
 
PPT
Introduction to regular expressions
Ben Brumfield
 
PPSX
Scala @ TomTom
Eric Bowman
 
PDF
Instaduction to instaparse
Alex Schoof
 
KEY
正規表現のいろは
Ayumu Hanba
 
KEY
Reg EX
Blazing Cloud
 
PDF
Scheme on WebAssembly: It is happening!
Igalia
 
PPT
Cleancode
hendrikvb
 
ODP
Introduction To Regex in Lasso 8.5
bilcorry
 
PDF
And now you have two problems. Ruby regular expressions for fun and profit by...
Codemotion
 
PPT
Regular expressions
Raj Gupta
 
PDF
Python (regular expression)
Chirag Shetty
 
PPTX
Ruby RegEx
Sarah Allen
 
PDF
Graph abstraction
openCypher
 
PPT
Php Chapter 4 Training
Chris Chubb
 
Introduction to Perl
Sway Wang
 
Regular Expressions in Javascript
Fen Slattery
 
regular-expressions lecture 28-string regular expression
smallboss311
 
FUNDAMENTALS OF REGULAR EXPRESSION (RegEX).pdf
Bryan Alejos
 
Regular expressions
Thomas Langston
 
Real life-coffeescript
David Furber
 
Introduction to regular expressions
Ben Brumfield
 
Scala @ TomTom
Eric Bowman
 
Instaduction to instaparse
Alex Schoof
 
正規表現のいろは
Ayumu Hanba
 
Scheme on WebAssembly: It is happening!
Igalia
 
Cleancode
hendrikvb
 
Introduction To Regex in Lasso 8.5
bilcorry
 
And now you have two problems. Ruby regular expressions for fun and profit by...
Codemotion
 
Regular expressions
Raj Gupta
 
Python (regular expression)
Chirag Shetty
 
Ruby RegEx
Sarah Allen
 
Graph abstraction
openCypher
 
Php Chapter 4 Training
Chris Chubb
 

Recently uploaded (20)

DOCX
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
PPTX
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
PPTX
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PDF
Become an Agentblazer Champion Challenge
Dele Amefo
 
PDF
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
PDF
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
PDF
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
PDF
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
PPTX
Presentation about variables and constant.pptx
kr2589474
 
PDF
Bandai Playdia The Book - David Glotz
BluePanther6
 
PPTX
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
PDF
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
PDF
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PPTX
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
PPTX
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
PDF
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
PPTX
TestNG for Java Testing and Automation testing
ssuser0213cb
 
The Future of Smart Factories Why Embedded Analytics Leads the Way
Varsha Nayak
 
Why Use Open Source Reporting Tools for Business Intelligence.pptx
Varsha Nayak
 
AI-Ready Handoff: Auto-Summaries & Draft Emails from MQL to Slack in One Flow
bbedford2
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
Become an Agentblazer Champion Challenge
Dele Amefo
 
Teaching Reproducibility and Embracing Variability: From Floating-Point Exper...
University of Rennes, INSA Rennes, Inria/IRISA, CNRS
 
Jenkins: An open-source automation server powering CI/CD Automation
SaikatBasu37
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Microsoft Teams Essentials; The pricing and the versions_PDF.pdf
Q-Advise
 
ShowUs: Pharo Stream Deck (ESUG 2025, Gdansk)
ESUG
 
Why Use Open Source Reporting Tools for Business Intelligence.pdf
Varsha Nayak
 
Presentation about variables and constant.pptx
kr2589474
 
Bandai Playdia The Book - David Glotz
BluePanther6
 
Presentation of Computer CLASS 2 .pptx
darshilchaudhary558
 
Solar Panel Installation Guide – Step By Step Process 2025.pdf
CRMLeaf
 
Appium Automation Testing Tutorial PDF: Learn Mobile Testing in 7 Days
jamescantor38
 
PFAS Reporting Requirements 2026 Are You Submission Ready Certivo.pptx
Certivo Inc
 
The-Dawn-of-AI-Reshaping-Our-World.pptxx
parthbhanushali307
 
The Role of Automation and AI in EHS Management for Data Centers.pdf
TECH EHS Solution
 
TestNG for Java Testing and Automation testing
ssuser0213cb
 

Regular expression in javascript

  • 1. toan @vnaking front-end developer at ticketbox.vn Regex in javascript
  • 2. Use Regular expressions are used to perform pattern- matching and "search-and-replace" functions on text.
  • 3. Basic /pattern/flags ● two forward slashes to show the beginning and end of the pattern ● flags: (optional) indicate how to search (modifier). Ex: /th/g (the g flag means "global" match - i.e. find all)
  • 4. Syntax (Anchors) ^ Matches the beginning of input ^a : “alice” not “banana” ^: “a linenand break” => 2 match $ Matches the end of input. a$ : “obama” not “name” . Matches any single character except a newline a.b : “a0b”, “acb” not “ab” b Word boundary bdogb : “dog”, “dog ” not “doggy” B Not word boundary BdogB : “doggy”, “adogg” not “dog”
  • 5. Syntax (Anchors) s Whitespace (space, tab, newline,...) sc: matches “a cat” not “xcat” S Not white space Sc: matches “cat” not “a cat” d Digit d: matches “1” in “1abc” D Not digit D: matches “b” in “123b” w Word (alphanumeric character including _) w: matches “a” in “a%” W Not word W: matches “%” in “a%”
  • 6. Syntax (Quantifiers) * Matches the preceding character 0 or more times zo* : “z” and “zoo” + Matches the preceding character 1 or more times zo+ : “zoo” not “z” ? Matches the preceding character 0 or 1 time ca?r : only “car” and “cr” {n} Matches exactly n times. o{2} : “fooood” not “bob” {n,} Matches at least n times o{2,} : “fooood” not “bob” {n,m} Matches the preceding character n to m times o{2,3} : “food”, “foood” not “fod”
  • 7. Syntax (Group & Range) | or (z|w)o : “zo” and “wo” (...) Matches subexpression and remembers the match (ab): “abc” -> 1st group match “ab” (?:...) Matches subexpression and do not remembers the match (the match doesn’t in array) (ab): “abc” -> No group (?=...) Matches only if there is a following pattern win(?=7): “win” in “win7” not “win95” (?!...) Matches only if there is not a following pattern win(?!=7): “win8” not “win7” [...] Range c[ma-c]: “cm”, “cb” not “cd” c[A-E0-5]: “cB”, “c1” not c6 [^...] Not in range c[^ab]: “cc” not “ca”
  • 8. Syntax (Modifier) g global match (find all) /is/g : “this is” => two match i Case-i-nse-nsitive /my/i : “My” and “my” m Multiple line /is/m : “this is”
  • 9. Replacement $& insert the whole regex match "1a2b".replace(/d+/g, "($&)") => "(1)a(2)b" $1,… $9 Insert the text matched by one of the first 9 capturing groups. "abc".replace(/(a)(b)(c)/g, "$3$2$1") => "cba" $` Insert the part of the subject string to the left of the regex match "abc".replace(/b/, "$`") => “aac” $' Insert the part of the subject string to the right of the regex match "abc".replace(/b/, "$'") => “acc”
  • 10. ● Creating ○ var re = new RegExp("pattern","flags"); or var re = /pattern/flags; ● Testing for matches: ○ /pattern/.test("this-is-a-string"): returns a Boolean telling you whether the string contains a match of the pattern ● Executes a search: ○ /pattern/.exec("this-is-a-string"): returns an array Creating a Regex
  • 11. ● Find all http in ticketbox’s url and change to https. ○ http://ticketbox.co.th -> https://ticketbox.co.th ○ http://www.ticketbox.vn -> https://www.ticketbox.vn ● find decremented event id by one for all ticketbox event url ○ https://ticketbox.vn/event/saigon-heat-vs-hitech-bangkok-city-34148 -> https: //ticketbox.vn/event/saigon-heat-vs-hitech-bangkok-city-34149 ● Find all date in format dd/mm/yyyy and convert to date ○ 17/11/2015 -> Tue, 17 Nov 2015 ● Swap firstname, lastname in a list of name (split by commas) ○ Toan Nguyen, Vu Phan, Dao Le -> Nguyen Toan, Phan Vu, Le Dao Practices
  • 12. Practice "http://ticketbox.vn/event/axwell-ingrosso-41450 http://ticketbox.co.th/saigon-heat-53982 https://ticketbox.co.th/event/fungjailoy-53838 http://TICKETBOX.vn/event/ingrosso-41451 https://ticketbox.vn/event/53982/ticket-booking http://acb.com/fungjailoy-53838".replace (https?://ticketbox.(?:vn|co.th)/event/(?:d|w|-)+(d+)s, function(a, b) { return a.replace(b, parseInt(b)+1)}); Output: "http://ticketbox.vn/event/axwell-ingrosso-41451 http://ticketbox.co.th/saigon-heat-53982 https://ticketbox.co.th/event/fungjailoy-53839 http://TICKETBOX.vn/event/ingrosso-41452 https://ticketbox.vn/event/53982/ticket-booking http://acb.com/fungjailoy-53838"