DEV Community

loading...
Cover image for Cheatsheet for the Regex Cheatsheet, Part VII: Groups & Ranges

Cheatsheet for the Regex Cheatsheet, Part VII: Groups & Ranges

Analogy | Absence | Example
The way I understand things is in relation to other things: Analogy: what can it be compared to? Absence: what would we have to do without it? Example: how does it work?
・3 min read

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

Alt Text

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

  1. Forward slashes go on either end like so: /something/
  2. Add g for "global" at the end to find every instance, like so: /something/g
  3. Add m to "multi line" to the beginning/end of each line, not just the beginning/end of each string, like /something/g or /something/gm

Groups & Ranges

. Any character except new line (\n)
  • . is used in /./g to 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'
]
Enter fullscreen mode Exit fullscreen mode

(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' ]
Enter fullscreen mode Exit fullscreen mode

[xyz] Range of characters (x or y or z)
  • [aeiou] is used in /[aeiou]/g to 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' ]
Enter fullscreen mode Exit fullscreen mode

[^xyz] Not a range of characters (x or y or z)
  • [^aeiou] is used in /[^aeiou]/g to 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'
]
Enter fullscreen mode Exit fullscreen mode

[x-z] Span from this character to that character (x through z)
  • [a-c] is used in /[a-c]/g to 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' ]
Enter fullscreen mode Exit fullscreen mode

Dunce Corner

\x Group/­sub­pattern 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...

Discussion (0)