https://www.reddit.com/r/ProgrammerHumor/comments/c9e0n4/regex_is_so_easy/
s/phone number/whatever you're looking for/gi
const re = /(?<exg>\d{3})-?(?<npa>\d{3})-?(?<nxx>\d{4})/;
const matches = re.exec('514-123-1234');
> console.log(matches.groups)
{exg: '514', npa: '123', nxx: '1234'}
> console.log(matches.groups.npa)
123
const re = /(?<person>\w+) is (.*?)(?=awesome)/g;
const matches = 'Sally is awesome\nBob is great\nKelly is also awesome'.matchAll(re)
> for (m of matches) console.log(m.groups.person)
Sally
Kelly
const re = /((?<person>\w+) (.*?) )(?!awesome)/g;
const matches = 'Sally is awesome\nBob is great\nKelly is awesome'.matchAll(re)
> for (m of matches) console.log(m.groups.person)
Bob
Positive
Negative
const re = /(?<person>\w+) \1/gi;
const matches = 'Bob bob\nJim bob\nkelly kelly'.matchAll(re);
> for (m of matches) console.log(m.groups.person)
Bob
kelly
https://programmerhumor.io/programming-memes/why-is-regex-so-annoying-and-hard-everytime-%F0%9F%A4%AF/
/<.*>/g
Let's find all HTML tags!
/is/
Let's find all instances of the word "is "
/[Cc]ats?/
Let's find all the cats!
/[A-z]/
Let's find all regular letters!
/\\d/
Let's find all the digits!
/\d*/
Let's find all the digits!
/bear|lion+/
Let's find all bears or lions!
Regex Denial of Service - A.K.A How to oops your prod
(a+)+
([a-zA-Z]+)*
(a|aa)+
(a|a?)+
(.*a){x} for x \> 10
const userName = untrustedRequestParams.username;
const password = untrustedRequestParams.password;
const testPassword = new RegExp(userName);
if (testPassword.test(password)) {
alert('I just took down prod');
} else {
alert('No really, I did.');
}
Examples of Evil™ regexes:
Did you know regex supports a comment mode??
const re = new RegExp(String.raw`
# match ASCII alpha-numerics
[a-zA-Z0-9]
`, "x");
pattern = """
^ # beginning of string
M{0,4} # thousands - 0 to 4 M's
(CM|CD|D?C{0,3}) # hundreds - 900 (CM), 400 (CD), 0-300 (0 to 3 C's),
# or 500-800 (D, followed by 0 to 3 C's)
(XC|XL|L?X{0,3}) # tens - 90 (XC), 40 (XL), 0-30 (0 to 3 X's),
# or 50-80 (L, followed by 0 to 3 X's)
(IX|IV|V?I{0,3}) # ones - 9 (IX), 4 (IV), 0-3 (0 to 3 I's),
# or 5-8 (V, followed by 0 to 3 I's)
$ # end of string
"""
re.search(pattern, 'M', re.VERBOSE)
Scarier example