James Sherry PRO
Web Development Tutor and Co-Founder of { The Jump } Digital School
aka RegExp or Regex
// /<pattern>/<flags>
const regex1 = /abc/gi;
// new RexExp(<pattern>, <flags>)
const regex2 = new RegExp('abc', 'gi');
// dynamically (You can't set flags using es6
// template strings, so...)
const searchTerm = 'cat';
const flags = 'gi';
const regex3 = new RegExp(searchTerm, flags);
(The delimiting slashes are the same direction as JS comments)
const re = /cat/i;
re.source //"^(\\d{3})(\\w+)$" or in this case 'cat'
re.lastIndex // 0
re.dotAll // false
re.flags // "i"
re.global // false
re.hasIndices // false
re.ignoreCase // true
re.multiline // false
re.sticky // false
re.unicode // false
From 2 perspectives:
This next slide will show both ways around, in the future you can just presume that you can do it from both the string and the regex perspective.
A regex for a concrete term (e.g. 'cat')
'Hello everyone!'
^ ^ ^ ^
'Hello everyone!'
^^^ ^^^^^^
"Hello cat!".match(/\bcat/); // starts with 'cat'
"Hello cat!".match(/cat\b/); // ends with 'cat'
"Hello cat!".match(/\bcat\b/); // has 'cat' as whole word
"Scatter!".match(/\Bcat/); // has 'cat' within it
N.B. They do not work with non-latin alphabets
By James Sherry
How to search strings for [abstract] patterns in JavaScript