SlideShare a Scribd company logo
Regular Expressions: JavaScript And Beyond
Regular Expressions:
JavaScript And Beyond

Max Shirshin

Frontend Team Lead
deltamethod
Introduction
Types of regular expressions
• POSIX (BRE, ERE)
• PCRE = Perl-Compatible Regular Expressions
From the JavaScript language specification:

"The form and functionality of regular expressions
is modelled after the regular expression facility in
the Perl 5 programming language".

4
JS syntax (overview only)
var re = /^foo/;
 
 

 
 

5
JS syntax (overview only)
var re = /^foo/;
// boolean
re.test('string');
 
 

6
JS syntax (overview only)
var re = /^foo/;
// boolean
re.test('string');
// null or Array
re.exec('string');

7
Regular expressions consist of...
●

Tokens

— common characters
— special characters (metacharacters)
●

Operations

— quantification
— enumeration
— grouping
8
Tokens and metacharacters
Any character
/./.test('foo');

// true

/./.test('rn')

// false

 
 
 
 

10
Any character
/./.test('foo');

// true

/./.test('rn')

// false

What do you need instead:
/[sS]/ for JavaScript
or
/./s (works in Perl/PCRE, not in JS)

11
String boundaries
>>> /^something$/.test('something')
true
 
 

 

 

12
String boundaries
>>> /^something$/.test('something')
true
>>> /^something$/.test('somethingnbad')
false
 

 

13
String boundaries
>>> /^something$/.test('something')
true
>>> /^something$/.test('somethingnbad')
false
>>> /^something$/m.test('somethingnbad')
true

14
Word boundaries
>>> /ba/.test('alabama)
true
 
 

 
 

 
 

15
Word boundaries
>>> /ba/.test('alabama)
true
>>> /ab/.test('alabama')
true
 
 

 
 

16
Word boundaries
>>> /ba/.test('alabama)
true
>>> /ab/.test('alabama')
true
>>> /ab/.test('naïve')
true
 
 

17
Word boundaries
>>> /ba/.test('alabama)
true
>>> /ab/.test('alabama')
true
>>> /ab/.test('naïve')
true
not a word boundary
/Ba/.test('alabama');
18
Character classes
Whitespace
/s/ (inverted version: /S/)
 
 

 
 

 
 

20

 
Whitespace
/s/ (inverted version: /S/)
FF:
t
u00a0
u2003
u2009

n
v
u1680 u180e
u2004 u2005
u200a u2028

Chrome, IE 9:
as in FF plus ufeff

f
r
u2000 u2001
u2006 u2007
u2029 u202f

IE 7, 8 :-(
only:
t n v f r u0020
21

u0020
u2002
u2008
u205f u3000
Alphanumeric characters
/d/ ~ digits from 0 to 9
/w/ ~ Latin letters, digits, underscore
Does not work for Cyrillic, Greek etc.

Inverted forms:
/D/ ~ anything but digits
/W/ ~ anything but alphanumeric characters

22
Custom character classes
Example:
/[abc123]/
 
 

 
 

 

23
Custom character classes
Example:
/[abc123]/
Metacharacters and ranges supported:
/[A-Fd]/
 
 

 

24
Custom character classes
Example:
/[abc123]/
Metacharacters and ranges supported:
/[A-Fd]/
More than one range is okay:
/[a-cG-M0-7]/
 

25
Custom character classes
Example:
/[abc123]/
Metacharacters and ranges supported:
/[A-Fd]/
More than one range is okay:
/[a-cG-M0-7]/
IMPORTANT: ranges come from Unicode, not
from national alphabets!
26
Custom character classes
"dot" means just dot!
/[.]/.test('anything') // false
 
 

27
Custom character classes
"dot" means just dot!
/[.]/.test('anything') // false
adding  ] /[]-]/

28
Inverted character classes
anything except a, b, c:
/[^abc]/
^ as a character:
/[abc^]/

29
Inverted character classes
/[^]/
matches ANY character;
a nice alternative to /[sS]/

30
Inverted character classes
/[^]/
matches ANY character;
could be
a nice alternative to /[sS]/

31
Inverted character classes
/[^]/
matches ANY character;
could be
a nice alternative to /[sS]/
Chrome, FF:
>>> /([^])/.exec('a');
['a', 'a']
32
Inverted character classes
/[^]/
matches ANY character;
could be
a nice alternative to /[sS]/
IE:
>>> /([^])/.exec('a');
['a', '']
33
Inverted character classes
/[^]/
matches ANY character;
could be
a nice alternative to /[sS]/
IE:
>>> /([sS])/.exec('a');
['a', 'a']
34
Quantifiers
Zero or more, one or more
/bo*/.test('b') // true
 

 

36
Zero or more, one or more
/bo*/.test('b') // true
/.*/.test('')
 

37

// true
Zero or more, one or more
/bo*/.test('b') // true
/.*/.test('')

// true

/bo+/.test('b') // false

38
Zero or one

/colou?r/.test('color');
/colou?r/.test('colour');

39
How many?
/bo{7}/
 

 

 
 

40

exactly 7
How many?
/bo{7}/

exactly 7

/bo{2,5}/

from 2 to 5, x < y

 

 
 

41
How many?
/bo{7}/

exactly 7

/bo{2,5}/

from 2 to 5, x < y

/bo{5,}/

5 or more

 
 

42
How many?
/bo{7}/

exactly 7

/bo{2,5}/

from 2 to 5, x < y

/bo{5,}/

5 or more

This does not work in JS:
/b{,5}/.test('bbbbb')
43
Greedy quantifiers
var r = /a+/.exec('aaaaa');
 
 

44
Greedy quantifiers
var r = /a+/.exec('aaaaa');
>>> r[0]
 

45
Greedy quantifiers
var r = /a+/.exec('aaaaa');
>>> r[0]
"aaaaa"

46
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
 
 

 
 
 

47
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
>>> r[0]
 

 
 
 

48
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
>>> r[0]
"a"
 
 
 

49
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
>>> r[0]
"a"
r = /a*?/.exec('aaaaa');
 
 

50
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
>>> r[0]
"a"
r = /a*?/.exec('aaaaa');
>>> r[0]
 

51
Lazy quantifiers
var r = /a+?/.exec('aaaaa');
>>> r[0]
"a"
r = /a*?/.exec('aaaaa');
>>> r[0]
""
52
Groups
Groups
capturing
/(boo)/.test("boo");
 
 

54
Groups
capturing
/(boo)/.test("boo");
non-capturing
/(?:boo)/.test("boo");

55
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
 
 
 
 
 

 
 
 
 

56
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
>>> RegExp.$1
"bo"
 
 
 

 
 
 
 

57
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
>>> RegExp.$1
"bo"
>>> RegExp.$2
"b"
 

 
 
 
 

58
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
>>> RegExp.$1
"bo"
>>> RegExp.$2
"b"
>>> RegExp.$9
""
 
 
 
 

59
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
>>> RegExp.$1
"bo"
>>> RegExp.$2
"b"
>>> RegExp.$9
""
>>> RegExp.$10
undefined
 
 

60
Grouping and the RegExp constructor
var result = /(bo)o+(b)/.exec('the booooob');
>>> RegExp.$1
"bo"
>>> RegExp.$2
"b"
>>> RegExp.$9
""
>>> RegExp.$10
undefined
>>> RegExp.$0
undefined
61
Numbering of capturing groups
/((foo) (b(a)r))/
 

 
 
 
62
Numbering of capturing groups
/((foo) (b(a)r))/
$1 (
 
 
 

63

)

foo bar
Numbering of capturing groups
/((foo) (b(a)r))/
$1 (
$2 (
 
 

64

)
)

foo bar
foo
Numbering of capturing groups
/((foo) (b(a)r))/
$1 (
$2 (
$3
 

65

)
)
(

)

foo bar
foo
bar
Numbering of capturing groups
/((foo) (b(a)r))/
$1 (
$2 (
$3
$4
66

)
)
(

)
( )

foo bar
foo
bar
a
Lookahead
var r = /best(?= match)/.exec('best match');
 
 

 
 

 
 

67
Lookahead
var r = /best(?= match)/.exec('best match');
>>> !!r
true
 
 

 
 

68
Lookahead
var r = /best(?= match)/.exec('best match');
>>> !!r
true
>>> r[0]
"best"
 
 

69
Lookahead
var r = /best(?= match)/.exec('best match');
>>> !!r
true
>>> r[0]
"best"
>>> /best(?! match)/.test('best match')
false

70
Lookbehind
NOT supported in JavaScript at all

/(?<=text)match/
positive lookbehind

/(?<!text)match/
negative lookbehind

71
Enumerations
Logical "or"
/red|green|blue light/
/(red|green|blue) light/
>>> /var a(;|$)/.test('var a')
true

73
Backreferences
true
/(red|green) apple is 1/.test('red apple is red')
true
/(red|green) apple is 1/.test('green apple is green')

74
Alternative character
represenations
Representing a character
x09 === t (not Unicode but ASCII/ANSI)
u20AC === € (in Unicode)
 

 
 

 
 

76
Representing a character
x09 === t (not Unicode but ASCII/ANSI)
u20AC === € (in Unicode)
backslash takes away special character
meaning:
/()/.test('()')
/n/.test('n')
 
 

77

// true
// true
Representing a character
x09 === t (not Unicode but ASCII/ANSI)
u20AC === € (in Unicode)
backslash takes away special character
meaning:
/()/.test('()')
/n/.test('n')

// true
// true

...or vice versa!
/f/.test('f') // false!
78
Flags
Regular expression flags
g i m s x y
 
 
 

 
 
 

80
Regular expression flags
g i m s x y
global match
 
 

 
 
 

81
Regular expression flags
g i m s x y
global match
ignore case
 

 
 
 

82
Regular expression flags
g i m s x y
global match
ignore case
multiline matching for ^ and $
 
 
 

83
Regular expression flags
g i m s x y
global match
ignore case
multiline matching for ^ and $
JavaScript does NOT provide support for:
string as single line
extend pattern

84
Regular expression flags
g i m s x y
global match
ignore case
multiline matching for ^ and $
Mozilla-only, non-standard:
sticky
Match only from the .lastIndex index (a
regexp instance property). Thus, ^ can
match at a predefined position.
85
Alternative syntax for flags
/(?i)foo/
/(?i-m)bar$/
/(?i-sm).x$/
/(?i)foo(?-i)bar/
Some implementations do NOT support flag
switching on-the-go.
In JS, flags are set for the whole regexp
instance and you can't change them.
86
RegExp in JavaScript
Methods
RegExp instances:
/regexp/.exec('string')
null or array ['whole match', $1, $2, ...]
/regexp/.test('string')
false or true
String instances:
'str'.match(/regexp/)
'str'.match('w{1,3}')
- same as /regexp/.exec if no 'g' flag used;
- array of all matches if 'g' flag used (internal
capturing groups ignored)
'str'.search(/regexp/)
'str'.search('w{1,3}')
first match index, or -1

88
Methods
String instances:
'str'.replace(/old/, 'new');
WARNING: special magic supported in the replacement string:
$$
inserts a dollar sign "$"
$&
substring that matches the regexp
$`
substring before $&
$'
substring after $&
$1, $2, $3 etc.:
string that matches n-th capturing group
'str'.replace(/(r)(e)gexp/g,
function(matched, $1, $2, offset, sourceString) {
// what should replace the matched part on this iteration?
return 'replacement';
});

89
RegExp injection
// BAD CODE
var re = new RegExp('^' + userInput + '$');
// ...
var userInput = '[abc]'; // oops!

// GOOD, DO IT AT HOME
RegExp.escape = function(text) {
return text.replace(/[-[]{}()*+?.,^$|#s]/g, "$&");
};
var re = new RegExp('^' + RegExp.escape(userInput) + '$');

90
Recommended reading
Online, just google it:
MDN Guide on Regular Expressions
The Book:

Mastering Regular Expressions
O'Reilly Media
Thank you!

More Related Content

What's hot (18)

DOCX
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
PDF
Beware: Sharp Tools
chrismdp
 
PPTX
PHP Strings and Patterns
Henry Osborne
 
PDF
Data Structure - 2nd Study
Chris Ohk
 
PDF
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
ODP
Programming in perl style
Bo Hua Yang
 
PDF
Implementing string
mohamed sikander
 
PDF
ES6: The future is now
Sebastiano Armeli
 
PDF
There's more than one way to empty it
Andrew Shitov
 
PDF
C lab programs
Dr. Prashant Vats
 
PPT
Cpp tutorial
Vikas Sharma
 
PDF
Functional pe(a)rls: Huey's zipper
osfameron
 
PDF
C++ programs
Mukund Gandrakota
 
DOC
Final ds record
Ganisius Ganish
 
PPT
Php Basic
Md. Sirajus Salayhin
 
PPT
Cquestions
mohamed sikander
 
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
Beware: Sharp Tools
chrismdp
 
PHP Strings and Patterns
Henry Osborne
 
Data Structure - 2nd Study
Chris Ohk
 
What's New in Perl? v5.10 - v5.16
Ricardo Signes
 
Programming in perl style
Bo Hua Yang
 
Implementing string
mohamed sikander
 
ES6: The future is now
Sebastiano Armeli
 
There's more than one way to empty it
Andrew Shitov
 
C lab programs
Dr. Prashant Vats
 
Cpp tutorial
Vikas Sharma
 
Functional pe(a)rls: Huey's zipper
osfameron
 
C++ programs
Mukund Gandrakota
 
Final ds record
Ganisius Ganish
 
Cquestions
mohamed sikander
 

Similar to Regular Expressions: JavaScript And Beyond (20)

PDF
Regular expressions
keeyre
 
PPT
Cleancode
hendrikvb
 
PPT
Perl Presentation
Sopan Shewale
 
PDF
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
PDF
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
PDF
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
PPT
Introduction to Perl
Sway Wang
 
PDF
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
PDF
Unicode Regular Expressions
Nova Patch
 
PDF
Coffee 'n code: Regexes
Phil Ewels
 
PDF
Regular expression for everyone
Sanjeev Kumar Jaiswal
 
PDF
Idiomatic Javascript (ES5 to ES2015+)
David Atchley
 
PDF
Good Evils In Perl
Kang-min Liu
 
PDF
Practical JavaScript Programming - Session 6/8
Wilson Su
 
PPTX
Groovy
Zen Urban
 
PPT
Ruby on Rails Intro
zhang tao
 
PDF
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
PDF
3.2 javascript regex
Jalpesh Vasa
 
PPT
Les origines de Javascript
Bernard Loire
 
PPT
Javascript
guest03a6e6
 
Regular expressions
keeyre
 
Cleancode
hendrikvb
 
Perl Presentation
Sopan Shewale
 
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Ruby presentasjon på NTNU 22 april 2009
Aslak Hellesøy
 
Introduction to Perl
Sway Wang
 
JavaScript - Chapter 9 - TypeConversion and Regular Expressions
WebStackAcademy
 
Unicode Regular Expressions
Nova Patch
 
Coffee 'n code: Regexes
Phil Ewels
 
Regular expression for everyone
Sanjeev Kumar Jaiswal
 
Idiomatic Javascript (ES5 to ES2015+)
David Atchley
 
Good Evils In Perl
Kang-min Liu
 
Practical JavaScript Programming - Session 6/8
Wilson Su
 
Groovy
Zen Urban
 
Ruby on Rails Intro
zhang tao
 
How to check valid Email? Find using regex.
Poznań Ruby User Group
 
3.2 javascript regex
Jalpesh Vasa
 
Les origines de Javascript
Bernard Loire
 
Javascript
guest03a6e6
 
Ad

Recently uploaded (20)

PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Ad

Regular Expressions: JavaScript And Beyond