🔰lookaround
🚧 under construction
(?=...) // Positive lookahead (match the position before the specified regex)
(?!...) // Negative lookahead (don’t match, as above)
(?<=...) // Positive lookbehind (match the position after the specified regex)
(?<!...) // Negative lookbehind (don't match, as above) const {log} = console;
/*
Symbols
-------
• ? : lookaround
• = : positive
• ! : negative (NOT)
• < : lookbehind
lookahead: X(?=Y)
------------------
match X (followed "immediately" by Y):
match X ("lookahead" for Y)
lookahead: X(?=Y)(?=Z)
-----------------------
match X (followed "immediately" by Y & Z)
match X ("lookahead" for Y & Z)
negative lookahead: X(?!Y)
---------------------------
match X (NOT followed "immediately" by Y)
match X ("negative lookahead" for Y)
Lookbehind: (?<=Y)X
--------------------
match X ("immediately" following Y)
Negative Lookbehind: (?<!Y)X
-----------------------------
match X (NOT "immediately" following Y)
*/
// ↱ space ╭╮
let str = "1 turkey costs $30€";
// ╰──── .*30 ─────╯
[
// lookahead
// ----------
// ╭X╮ Y
str.match(/\d+(?=€)/), // ["30"]
// ╭X╮ ╭╮ ╭──╮
str.match(/\d+(?=\s)(?=.*30)/), // ["1"]
str.match(/\d+(?=.*30)(?=\s)/), // ["1"]
str.match(/\d+(?=30)(?=\s)/), // ⭐️ null
// (no digits are followed "immediately" by "30")
// negative lookahead
// ------------------
str.match(/\d+\b(?!€)/g), // ["1"]
// ╰─╯╰╯ ↑
// lookbehind:
// ----------
// escaped $ ➞╭╮ ╭─╮
str.match(/(?<=\$)\d+/), // ["30"]
].forEach(x => log(x))examples
Password Validation
words with (-) or (')
Last updated