DEV Community

Cover image for Cheatsheet for the Regex Cheatsheet, Part VII: Groups & Ranges
Analogy | Absence | Example
Analogy | Absence | Example

Posted on

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

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...

Top comments (0)