Regular Expressions — Programming with Text

cmdlhz’s 2019 Self-learning Story #5

Hyejung Lim
6 min readApr 11, 2019
Photo by Andy Hall on Unsplash

[cmdlhz’s 2019 Self-leaching Story] Series

[JavaScript]
🌱 Net Ninja - JavaScript ES6
🌱 Net Ninja - Asynchronous JavaScript
🌱 Net Ninja - Object Oriented JavaScript
🌱 Coding Train - JavaScript ES6-ES8 (Part1, Part2)
[Regular Expressions]
🌱 Coding Train - Programming with Text
[Vue]
🌱 Net Ninja - Vue CLI3
🌱 Net Ninja - Vue JS2
🌱 Net Ninja - Vuex
🌱 Net Ninja - Vuetify (Part1, Part2, Part3)
[Python/Django]
🌱 Net Ninja - Python3
🌱 Net Ninja - Django
[Figma]
🌱 Design Course
🌱 Figma (Part1, Part2, Part3, Part4, Part5)
[Illustrator]
🌱 TastyTuts (Part1, Part2, Part3)
[SQL]
🌱 freeCodeCamp

🌱 This is my summary of Coding Train’s “Regular Expressions — Programming with Text” on YouTube.

[1. Intro]
[2. Meta-characters]
[3. Character Classes]
[4. Capturing Groups]
[5. Back References]
[6. test() & match()]
[7. exec()]
[8. split()]
[9. replace()]

[ 1. Intro ]

>>> To search for regex in Visual Studio Code, press cmd/ctrl+ F and then click .*

[ 2. Meta-characters ]

https://regex101.com/ : Test regular expressions and check out quick reference.

Some of meta-characters are the following:

TYPE 1: Single Characters

. : Matches any character other than newline (or including newline with the /s flag).

\s : Matches any space, tab or newline character.

\S : Matches anything other than a space, tab or newline.

\d : Matches any decimal digit. Equivalent to [0–9].

\D : Matches anything other than a decimal digit.

\w : Matches any letter, digit or underscore. Equivalent to [a-zA-Z0–9_].

\W : Matches anything other than a letter, digit or underscore.

(a|b) : Matches the a or the b part of the subexpression.

\ : This may be used to match the literal value of any meta-character, or the / delimiter.

TYPE 2: Quantifiers

a* : Matches zero or more consecutive `a` characters.

a+ : Matches one or more consecutive `a` characters.

a? : Matches an `a` character or nothing.

a{3} : Matches exactly 3 consecutive `a` characters.

a{3,} : Matches at least 3 consecutive `a` characters.

a{3,6} : Matches between 3 and 6 (inclusive) consecutive `a` characters.

TYPE 3: Position

^ : Matches the start of a string without consuming any characters. If multiline mode is used, this will also match immediately after a newline character.

$ : Matches the end of a string without consuming any characters. If multiline mode is used, this will also match immediately before a newline character.

\b : Matches, without consuming any characters, immediately between a character matched by \w and a character not matched by \w (in either order). It cannot be used to separate non words from words.

TYPE 4: Flags

g : Tells the engine not to stop after the first match has been found, but rather to continue until no more matches can be found.

m : The ^ and $ anchors now match at the beginning/end of each line respectively, instead of beginning/end of the entire string.

i : A case insensitive match is performed, meaning capital letters will be matched by non-capital letters and vice versa.

[ 3. Character Classes ]

[abc] : Matches either an a, b or c character

[a-z] : Matches any characters between a and z, including a and z.

[^a-z] : Matches any characters except those in the range a-z.

EXAMPLE :

[ 4. Capturing Groups ]

You can group regular expressions and replace them.

EXAMPLE 1 :

EXAMPLE 2 :

EXAMPLE 3 :

[ 6. test() & match() ]

“The test() method executes a search for a match between a regular expression and a specified string. Returns true or false” (MDN).

“The match() method retrieves the result of matching a string against a regular expression” (MDN).

[ 7. exec() ]

“The exec() method executes a search for a match in a specified string. Returns a result array, or null” (MDN)

>>> “exec with a global regular expression is meant to be used in a loop, as it will still retrieve all matched subexpressions” (Ry-)

[ 8. split() ]

[ 9. replace() ]

EXAMPLE 1 :

EXAMPLE 2 :

Thanks for reading! 🎵 If you like this blog post, please clap👏

--

--