-
-
Save knownothingsnow/71710207999244b504ff0b05e3cf2bc7 to your computer and use it in GitHub Desktop.
A regex cheatsheet 👩🏻💻 (by Catherine)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let regex; | |
/* 匹配特定的字符串 */ | |
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"); // 用第二个参数取代匹配 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment