Skip to content

Instantly share code, notes, and snippets.

@himanshurajora
Created January 22, 2024 10:00
Show Gist options
  • Save himanshurajora/310432fcc7e9e4e941e661a253971892 to your computer and use it in GitHub Desktop.
Save himanshurajora/310432fcc7e9e4e941e661a253971892 to your computer and use it in GitHub Desktop.
Regex discussion on Adevent of Code 2023 - Day 1 Part 2
const regex = /(\d)/gmi;
const regex2 = new RegExp(`(?=(?<digit>[0-9]|one|two|three|four|five|six|seven|eight|nine|zero))`, 'gmi')
const input = 'afsdone2f537826sdf5sixf';
const letters = {
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
zero: 0
}
const r3 = +'5'
r3
const result = [...input.matchAll(regex2)]
const first = result[0].groups.digit
const r1 = isNaN(+first) && letters[first];
r1
const second = result[result.length - 1].groups.digit
const r2 = isNaN(+second) && letters[second];
r2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment