Free Code Camp RegularExpression
Regex
javascript用regex.test(“string”)来验证是否match,
也可以用ruby的“STRING”.match(regex) 的方式
/regex|match|test/.test(“regex match test”) will return true;
/regex/i : 这个i就是ignore case,不看大小写, /g 就是repeat
reg = /REGEX/i
reg.text("regex") => truely value“STRING".match(reg) => falsy valuereg = /REGEX/g"REGEX REGEX REGEX".match(reg) 会返回一个array,regex出现了几次,array就会有几个regex的元素。
“.” dot=> wildcard 可以代表任何一个字符,只能代表一个: “fu.” 就是match fu就可以了,其他不管。
[a-z] all letter
“+” match one or more times
“*” match zero or more times
“?” lazy match, /t.*?I/ will match the shortest match instead of the longest(greedy).
“^” in the “[]” it means “not”, outside of “[]” it means “start of the string”
“\w” word => /[A-Za-z0–9_]/
“\W” not word => /[^A-Za-z0–9_]/
“\d” digits => /[0–9]/
“\D” not digit => /[^0–9]/
“\s” => space “ ” return “\n” tab “\t”
“\S” => not space characters
- **********
- “{start, end}” range of characters => /he{1,3}llo/ will match “hello” “heello” “heeello”
- “{start, }” more than start => /he{1,}llo/ will match “hello, heello, heeello, heeeello….”
- “{num}” match exactly “num” characters.
- “?” 在之前接在“*” 之后的时候可以表示shortest match,但是在这里我们让它表示为0 or 1 existense : /colou?r/ 这个时候“u”就是可有可无的了
- **********
- ********
- 不太理解:look ahead: ?=; ?!=
- ********
?=\w{5} 是否有5个char
?=\w*\d{2} 是否有两个digits
”F(ab|cd)k” => Fabk Fcdk 都会match
- ************************
- 非常重要
- ************************
- regex = /(\w+)\s\1\s(abc)\s\2/
- 1 代表着(\w+) 这部分内容,而2 代表着(abc)这部分内容
“string word”.replace(/(\w+)\s(\w+)/, ‘$2, $1’
这行code会调换string 和 word,所以在replace function中,$1 代表第一个括号中的内容,$2 代表第二个括号中的内容。
string.replace(/regex/, “somestring)
可行,注意/g