Skip to content

Instantly share code, notes, and snippets.

@knownothingsnow
Forked from cxa/regexCheatsheet.js
Created March 8, 2020 11:03
Show Gist options
  • Select an option

  • Save knownothingsnow/71710207999244b504ff0b05e3cf2bc7 to your computer and use it in GitHub Desktop.

Select an option

Save knownothingsnow/71710207999244b504ff0b05e3cf2bc7 to your computer and use it in GitHub Desktop.

Revisions

  1. @cxa cxa revised this gist Jan 17, 2019. 1 changed file with 82 additions and 82 deletions.
    164 changes: 82 additions & 82 deletions regexCheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -1,84 +1,84 @@
    let regex;

    /* matching a specific string */
    regex = /hello/; // looks for the string between the forward slashes (case-sensitive)... matches "hello", "hello123", "123hello123", "123hello"; doesn't match for "hell0", "Hello"
    regex = /hello/i; // looks for the string between the forward slashes (case-insensitive)... matches "hello", "HelLo", "123HelLO"
    regex = /hello/g; // looks for multiple occurrences of string between the forward slashes...

    /* wildcards */
    regex = /h.llo/; // the "." matches any one character other than a new line character... matches "hello", "hallo" but not "h\nllo"
    regex = /h.*llo/; // the "*" matches any character(s) zero or more times... matches "hello", "heeeeeello", "hllo", "hwarwareallo"

    /* shorthand character classes */
    regex = /\d/; // matches any digit
    regex = /\D/; // matches any non-digit
    regex = /\w/; // matches any word character (a-z, A-Z, 0-9, _)
    regex = /\W/; // matches any non-word character
    regex = /\s/; // matches any white space character (\r (carriage return),\n (new line), \t (tab), \f (form feed))
    regex = /\S/; // matches any non-white space character

    /* specific characters */
    regex = /[aeiou]/; // matches any character in square brackets
    regex = /[ck]atherine/; // matches catherine or katherine
    regex = /[^aeiou]/; // matches anything except the characters in square brackets

    /* character ranges */
    regex = /[a-z]/; // matches all lowercase letters
    regex = /[A-Z]/; // matches all uppercase letters
    regex = /[e-l]/; // matches lowercase letters e to l (inclusive)
    regex = /[F-P]/; // matches all uppercase letters F to P (inclusive)
    regex = /[0-9]/; // matches all digits
    regex = /[5-9]/; // matches any digit from 5 to 9 (inclusive)
    regex = /[a-zA-Z]/; // matches all lowercase and uppercase letters
    regex = /[^a-zA-Z]/; // matches non-letters

    /* matching repetitions using quantifiers */
    regex = /(hello){4}/; // matches "hellohellohellohello"
    regex = /hello{3}/; // matches "hellooo" and "helloooo" but not "helloo"
    regex = /\d{3}/; // matches 3 digits ("312", "122", "111", "12312321" but not "12")
    regex = /\d{3,7}/; // matches digits that occur between 3 and 7 times (inclusive)
    regex = /\d{3,}/; // matches digits that occur at least 3 times

    /* matching repetitions using star and plus */
    regex = /ab*c/; // matches zero or more repetitions of "b" (matches "abc", "abbbbc", "ac")
    regex = /ab+c/; // matches one or more repetitions of "b" (matches "abc", "abbbbc", but not "ac")

    /* matching beginning and end items */
    regex = /^[A-Z]\w*/; // matches "H", "Hello", but not "hey"
    regex = /\w*s$/; // matches "cats", "dogs", "avocados", but not "javascript"

    /* matching word boundaries
    positions of word boundaries:
    1. before the first character in string (if first character is a word character)
    2. after the last character in the string, if the last character is a word character
    3. between two characters in string, where one is a word character and the other isn't */
    regex = /\bmeow\b/; // matches "hey meow lol", "hey:meow:lol", but not "heymeow lol"

    /* groups */
    regex = /it is (ice )?cold outside/; // matches "it is ice cold outside" and "it is cold outside"
    regex = /it is (?:ice )?cold outside/; // same as above except it is a non-capturing group
    regex = /do (cats) like taco \1/; // matches "do cats like taco cats"
    regex = /do (cats) like (taco)\? do \2 \1 like you\?/; // matches "do cats like taco? do taco cats like you?"

    //branch reset group (available in Perl, PHP, R, Delphi... commented out because this is a js file)
    // regex = /(?|(cat)|(dog))\1/; // matches "catcat" and "dogdog"

    /* alternative matching */
    regex = /i like (tacos|boba|guacamole)\./; // matches "i like tacos.", "i like boba.", and "i like guacamole."

    /* forward reference (available in Perl, PHP, Java, Ruby, etc... commented out because this is a js file) */
    // regex = /(\2train|(choo))+/; // matches "choo", "choochoo", "chootrain", choochootrain", but not "train"

    /* lookaheads */
    regex = /z(?=a)/; // positive lookahead... matches the "z" before the "a" in pizza" but not the first "z"
    regex = /z(?!a)/; // negative lookahead... matches the first "z" but not the "z" before the "a"

    /* lookbehinds */
    regex = /(?<=[aeiou])\w/; // positive lookbehind... matches any word character that is preceded by a vowel
    regex = /(?<![aeiou])\w/; // negative lookbehind... matches any word character that is not preceded by a vowel

    /* functions I find useful */
    regex.test("hello"); // returns true if found a match, false otherwise
    regex.execute("hello"); // returns result array, null otherwise
    "football".replace(/foot/,"basket"); // replaces matches with second argument
    /* 匹配特定的字符串 */
    regex = /hello/; // 查找斜杠中的字符串(大小写敏感)……匹配 "hello", "hello123", "123hello123", "123hello",但不匹配 "hell0", "Hello"
    regex = /hello/i; // 查找斜杠中的字符串(大小写不敏感)……匹配 "hello", "HelLo", "123HelLO"
    regex = /hello/g; // 全局查找斜杠中的字符串……

    /* 通配符 */
    regex = /h.llo/; // "." 匹配除了换行外的任何一个字符……匹配 "hello", "hallo",但不匹配 "h\nllo"
    regex = /h.*llo/; // "*" 匹配任何字符零次或多次,如 "hello", "heeeeeello", "hllo", "hwarwareallo"

    /* 简略字符类 */
    regex = /\d/; // 匹配数字
    regex = /\D/; // 匹配非数字
    regex = /\w/; // 匹配单词字符 (a-z, A-Z, 0-9, _)
    regex = /\W/; // 匹配非单词字符
    regex = /\s/; // 匹配空白字符(\r (回车),\n (换行), \t (制表符), \f (换页符))
    regex = /\S/; // 匹配非空白字符

    /* 特定字符 */
    regex = /[aeiou]/; // 匹配方括号中的任意字符
    regex = /[ck]atherine/; // 匹配 catherine katherine
    regex = /[^aeiou]/; // 匹配除方括号内以外的字符

    /* 字符范围 */
    regex = /[a-z]/; // 匹配小写字母
    regex = /[A-Z]/; // 匹配大写字母
    regex = /[e-l]/; // 匹配从 e 到 l 的字母
    regex = /[F-P]/; // 匹配 F 到 P 的字母
    regex = /[0-9]/; // 匹配数字
    regex = /[5-9]/; // 匹配 5 至 9 的数字
    regex = /[a-zA-Z]/; // 匹配大小写字母
    regex = /[^a-zA-Z]/; // 匹配非字母

    /* 使用量词重复匹配 */
    regex = /(hello){4}/; // 匹配 "hellohellohellohello"
    regex = /hello{3}/; // 匹配 "hellooo" "helloooo",但不匹配 "helloo"
    regex = /\d{3}/; // 匹配三个数字 (匹配 "312", "122", "111", "12312321",但不匹配 "12")
    regex = /\d{3,7}/; // 匹配三到七个数字
    regex = /\d{3,}/; // 匹配至少三个数字

    /* 使用星号和加号匹配重复 */
    regex = /ab*c/; // 匹配零个或多个重复的 "b"(匹配 "abc", "abbbbc", "ac"
    regex = /ab+c/; // 匹配一个或多个重复的 "b"(匹配 "abc", "abbbbc", 但不匹配 "ac"

    /* 匹配开头或结尾的项目 */
    regex = /^[A-Z]\w*/; // 匹配 "H", "Hello", 但不匹配 "hey"
    regex = /\w*s$/; // 匹配 "cats", "dogs", "avocados", 但不匹配 "javascript"

    /* 匹配单词分界
    单词分界位置
    1. 首字符是单词字符的字符串之前
    2. 末字符是单词字符的字符串之后
    3. 两个字符间,其中一个属于单词字符而另一个不是 */
    regex = /\bmeow\b/; // 匹配 "hey meow lol", "hey:meow:lol", 但不匹配 "heymeow lol"

    /* 分组 */
    regex = /it is (ice )?cold outside/; // 匹配 "it is ice cold outside" "it is cold outside"
    regex = /it is (?:ice )?cold outside/; // 跟上面一样,但这是一个非捕获分组
    regex = /do (cats) like taco \1/; // 匹配 "do cats like taco cats"
    regex = /do (cats) like (taco)\? do \2 \1 like you\?/; // 匹配 "do cats like taco? do taco cats like you?"

    //分支重置分组(Perl, PHP, R, Delphi 等语言支持此功能,JS 不支持,因此注释掉)
    // regex = /(?|(cat)|(dog))\1/; // 匹配 "catcat" "dogdog"

    /* 多选匹配 */
    regex = /i like (tacos|boba|guacamole)\./; // 匹配 "i like tacos.", "i like boba.", and "i like guacamole."

    /* 向前引用(Perl, PHP, R, Delphi 等语言支持此功能,JS 不支持,因此注释掉)*/
    // regex = /(\2train|(choo))+/; // 匹配 "choo", "choochoo", "chootrain", choochootrain", 但不匹配 "train"

    /* 顺序环视 */
    regex = /z(?=a)/; // 肯定型顺序环视……匹配在 "a" 之前的 "z" (比如 pizza),但不匹配首字符 "z"
    regex = /z(?!a)/; // 否定型顺序环视……匹配首字符 "z",除了在 "a" 之前的 "z"

    /* 逆序环视 */
    regex = /(?<=[aeiou])\w/; // 肯定型逆序环视……匹配在元音后的单词字符
    regex = /(?<![aeiou])\w/; // 否定型逆序环视……匹配不在原因后的单词字符

    /* 我觉得很有用的函数 */
    regex.test("hello"); // 如果有匹配则返回真
    regex.execute("hello"); // 有匹配返回数组,反之为 null
    "football".replace(/foot/, "basket"); // 用第二个参数取代匹配
  2. @sarthology sarthology created this gist Jan 10, 2019.
    84 changes: 84 additions & 0 deletions regexCheatsheet.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    let regex;

    /* matching a specific string */
    regex = /hello/; // looks for the string between the forward slashes (case-sensitive)... matches "hello", "hello123", "123hello123", "123hello"; doesn't match for "hell0", "Hello"
    regex = /hello/i; // looks for the string between the forward slashes (case-insensitive)... matches "hello", "HelLo", "123HelLO"
    regex = /hello/g; // looks for multiple occurrences of string between the forward slashes...

    /* wildcards */
    regex = /h.llo/; // the "." matches any one character other than a new line character... matches "hello", "hallo" but not "h\nllo"
    regex = /h.*llo/; // the "*" matches any character(s) zero or more times... matches "hello", "heeeeeello", "hllo", "hwarwareallo"

    /* shorthand character classes */
    regex = /\d/; // matches any digit
    regex = /\D/; // matches any non-digit
    regex = /\w/; // matches any word character (a-z, A-Z, 0-9, _)
    regex = /\W/; // matches any non-word character
    regex = /\s/; // matches any white space character (\r (carriage return),\n (new line), \t (tab), \f (form feed))
    regex = /\S/; // matches any non-white space character

    /* specific characters */
    regex = /[aeiou]/; // matches any character in square brackets
    regex = /[ck]atherine/; // matches catherine or katherine
    regex = /[^aeiou]/; // matches anything except the characters in square brackets

    /* character ranges */
    regex = /[a-z]/; // matches all lowercase letters
    regex = /[A-Z]/; // matches all uppercase letters
    regex = /[e-l]/; // matches lowercase letters e to l (inclusive)
    regex = /[F-P]/; // matches all uppercase letters F to P (inclusive)
    regex = /[0-9]/; // matches all digits
    regex = /[5-9]/; // matches any digit from 5 to 9 (inclusive)
    regex = /[a-zA-Z]/; // matches all lowercase and uppercase letters
    regex = /[^a-zA-Z]/; // matches non-letters

    /* matching repetitions using quantifiers */
    regex = /(hello){4}/; // matches "hellohellohellohello"
    regex = /hello{3}/; // matches "hellooo" and "helloooo" but not "helloo"
    regex = /\d{3}/; // matches 3 digits ("312", "122", "111", "12312321" but not "12")
    regex = /\d{3,7}/; // matches digits that occur between 3 and 7 times (inclusive)
    regex = /\d{3,}/; // matches digits that occur at least 3 times

    /* matching repetitions using star and plus */
    regex = /ab*c/; // matches zero or more repetitions of "b" (matches "abc", "abbbbc", "ac")
    regex = /ab+c/; // matches one or more repetitions of "b" (matches "abc", "abbbbc", but not "ac")

    /* matching beginning and end items */
    regex = /^[A-Z]\w*/; // matches "H", "Hello", but not "hey"
    regex = /\w*s$/; // matches "cats", "dogs", "avocados", but not "javascript"

    /* matching word boundaries
    positions of word boundaries:
    1. before the first character in string (if first character is a word character)
    2. after the last character in the string, if the last character is a word character
    3. between two characters in string, where one is a word character and the other isn't */
    regex = /\bmeow\b/; // matches "hey meow lol", "hey:meow:lol", but not "heymeow lol"

    /* groups */
    regex = /it is (ice )?cold outside/; // matches "it is ice cold outside" and "it is cold outside"
    regex = /it is (?:ice )?cold outside/; // same as above except it is a non-capturing group
    regex = /do (cats) like taco \1/; // matches "do cats like taco cats"
    regex = /do (cats) like (taco)\? do \2 \1 like you\?/; // matches "do cats like taco? do taco cats like you?"

    //branch reset group (available in Perl, PHP, R, Delphi... commented out because this is a js file)
    // regex = /(?|(cat)|(dog))\1/; // matches "catcat" and "dogdog"

    /* alternative matching */
    regex = /i like (tacos|boba|guacamole)\./; // matches "i like tacos.", "i like boba.", and "i like guacamole."

    /* forward reference (available in Perl, PHP, Java, Ruby, etc... commented out because this is a js file) */
    // regex = /(\2train|(choo))+/; // matches "choo", "choochoo", "chootrain", choochootrain", but not "train"

    /* lookaheads */
    regex = /z(?=a)/; // positive lookahead... matches the "z" before the "a" in pizza" but not the first "z"
    regex = /z(?!a)/; // negative lookahead... matches the first "z" but not the "z" before the "a"

    /* lookbehinds */
    regex = /(?<=[aeiou])\w/; // positive lookbehind... matches any word character that is preceded by a vowel
    regex = /(?<![aeiou])\w/; // negative lookbehind... matches any word character that is not preceded by a vowel

    /* functions I find useful */
    regex.test("hello"); // returns true if found a match, false otherwise
    regex.execute("hello"); // returns result array, null otherwise
    "football".replace(/foot/,"basket"); // replaces matches with second argument