Intro to REGEX
1.2.2
Consider this code without RegEx:
const isOnlyAlphaNumeric = (str) => { if (!str.length) return false; const valid = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'; for (let i = 0; i < str.length; i++) { if (!valid.includes(str[i])) { return false; } } return true; }; console.log(isOnlyAlphaNumeric('Hello world!')); // false console.log(isOnlyAlphaNumeric('Hello_world')); // true |
Here’s the RegEx version:
const isOnlyAlphaNumeric = (str) => /^\w+$/.test(str); |
RegEx is a perfect example of using the right tool for right the job
Don’t be intimidated!
RegEx = Regular Expressions
RegEx is about matching patterns in strings
Let’s get started!
Heads up!
We’re going to move quickly
to expose you to all the basics.
Focus on concepts
There are cheat sheets later!
Letters
/Hello there/
Exact
letter
matches
Flags: case insensitive
/hello there/i
Modify
the RegEx’s
default
behavior
Flags: global
/e/g
Match
every
instance
Flags: global + insensitive
/h/gi
Combine
and
conquer
Character set: [ ]
/[aeiou]/gi
Match any
single character
from a set of characters
Negated characters: [^]
/[^aeiou]/gi
Avoid the
characters
1 or more: +, 0 or more: *, 0 or 1: ?
/[^aeiou]+/gi
These are the
wildcards
quantifier: {x}, {x, }, {x, y}
/[^aeiou]{2}/gi
Match only
if the numbers
are exact
We need better matchers
[0-9], [0-20]
[a-z], [a-d]
[A-Z]
[a-zA-Z]
\d
\D
\w
\W
\s
\S
\b
\B
.
\.
Any single digit, any in range
Any lowercase letter, any in range
Any uppercase letter
Any letter
Any digit (0-9)
Any NON digit
Any alphanumeric (and _ )
Any NON alphanumeric
Any whitespace character
Any NON whitespace character
Any word break
Any NON break
Any character
An “escaped” literal period
16
Special
Character Classes/Sets
Putting it together: all length 2 sequences of non-vowel letters
/[^aeiou\s]{2}/gi
Groups are for multiple, specific
characters,
sets are for one
groups: ( ), (this|or|that)
/(cat|dog)/gi
anchors: ^, $
/^i.+!$/gi
Match only
if the numbers
are exact
Whew, that was a lot!
Take 10 minutes to play around
in scriptular.com on your own
before we add JS into the mix.
We slacked the cheat sheet!
g = global, i = case insensitive
You know what letters are
0 to 9
[ ]
[^]
*
+
?
^, $
{2} {2,} {2,4}
( ) (this|or|that)
flags
letters
digits
character class/set
Negated set
0 or more
1 or more
0 or 1
start of string, end of string
quantifier
groups
21
Cheat
Sheet
Using RegEx with
JAVASCRIPT
Creating a new RegEx
New RegExp (don’t forget the p)
Literal syntax
Clunky, but dynamic
Succinct, but static
const literal = /[^a-z]/g;�const wrong = /`${name}`/; // won't be dynamic |
const regEx = new RegExp('ab', 'g');�const greet = new RegExp(`Hi ${name}`, 'g'); |
When using RegExp,
you must escape the “\” characters!
“\” are escape characters,
to use them literally, escape them: “\\”
const brokenRegEx = new RegExp(`\w\d`, 'g');�console.log(brokenRegEx);�// /wd/g - the \ are ignored��const goodRegEx = new RegExp(`\\w\\d`, 'g');�console.log(goodRegEx); // /\w\d/g |
The .test() RegEx method
console.log(/[a-z]/.test('Hello!')); // true�console.log(/\d/.test('Hello!')); // false |
Now,
Let’s try writing the function from the opening ourselves!
const isOnlyAphaNumeric = (str) => /^\w+$/.test(str); |
So clean
Next Steps:
For your HW, you’ll need to use the .match and .replace methods.
Check them out on your own!