Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 23, 2021 02:21 am GMT

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 (
)
  • . 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']
(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]/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' ]
[^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']
[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' ]

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


Original Link: https://dev.to/mathlete/cheatsheet-for-the-regex-cheatsheet-part-vii-groups-ranges-32ne

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To