Intro
I was recently doing a code challenge for a job interview that required me to strip out all nonalphabetic characters. "Ah! I should use Regular Expressions for this!" I thought in triumph, impressed that I even knew what regular expressions were. That fleeting moment of glory faded once I decided to brush up on regular expressions and landed on the encouragingly-named Regular Expressions Cheatsheet. I had no idea how to use it!
So, for people like me, here is a Cheatsheet for the Regular Expressions Cheatsheet, Part VII: Groups & Ranges
What's are Groups & Ranges?
It does exactly what it says on the tin, as they say. Groups of characters and ranges of characters.
Anatomy of a regular expression
- Forward slashes go on either end like so:
/something/ - Add
gfor "global" at the end to find every instance, like so:/something/g - Add
mto "multi line" to the beginning/end of each line, not just the beginning/end of each string, like/something/gor/something/gm
Groups & Ranges
. Any character except new line (\n)
-
.is used in/./gto find the following: The lion roared⮐
Again - Example on regex101.com
- Example in Javascript:
let sentence = "The lion roared";
let regex = /./g;
let found = sentence.match(regex);
console.log(found); // [
'T', 'h', 'e', ' ', 'l',
'i', 'o', 'n', ' ', 'r',
'o', 'a', 'r', 'e', 'd',
' ', ' ', 'A', 'g', 'a',
'i', 'n'
]
(x|y) This character or that character (x or y)
-
(a|b)is used in/(a|b)/to find the following: The lion roared - Example on regex101.com
- Example in Javascript:
let sentence = "The lion roared";
let regex = /(a|b)/;
let found = sentence.match(regex);
console.log(found); // [ 'a' ]
[xyz] Range of characters (x or y or z)
-
[aeiou]is used in/[aeiou]/gto find the following: The lion roared - Example on regex101.com
- Example in Javascript:
let sentence = "The lion roared";
let regex = /[aeiou]/g;
let found = sentence.match(regex);
console.log(found); // [ 'e', 'i', 'o', 'o', 'a', 'e' ]
[^xyz] Not a range of characters (x or y or z)
-
[^aeiou]is used in/[^aeiou]/gto find the following: The lion roared - Example on regex101.com
- Example in Javascript:
let sentence = "The lion roared";
let regex = /[^aeiou]/g;
let found = sentence.match(regex);
console.log(found); // [
'T', 'h', ' ',
'l', 'n', ' ',
'r', 'r', 'd'
]
[x-z] Span from this character to that character (x through z)
-
[a-c]is used in/[a-c]/gto find the following: The lion roared a bunch of times - Example on regex101.com
- Example in Javascript:
let sentence = "The lion roared a bunch of times";
let regex = /[a-c]/g;
let found = sentence.match(regex);
console.log(found); // [ 'a', 'a', 'b', 'c' ]
Dunce Corner
\x Group/subpattern number "x"
I don't get this. MDN Web Docs says:
A back reference to the last substring matching the n parenthetical in the regular expression (counting left parentheses). For example, /apple(,)\sorange\1/ matches "apple, orange," in "apple, orange, cherry, peach".
I have read this about five times and I still don't know what they're talking about. But I also have low blood sugar at the moment, so...

Top comments (0)