let testStr = "freeCodeCamp"; let testRegex = /Code/; testRegex.test(testStr); // true
2.2 用|匹配多个字符串
1 2 3 4 5 6 7
let petString = "James has a pet cat."; let petString2 = "James has a pet dog."; let petRegex = /dog|cat|bird|fish/; let result = petRegex.test(petString); // true let result2 = petRegex.test(petString2); // true
2.3 忽略大小写i
1 2 3
let myString = "freeCodeCamp"; let fccRegex = /freeCodeCamp/i; let result = fccRegex.test(myString);
2.4 用match提取匹配的字符串
1 2 3 4 5 6
"Hello, World!".match(/Hello/); // Returns ["Hello"] let ourStr = "Regular expressions"; let ourRegex = /expressions/; ourStr.match(ourRegex); // Returns ["expressions"]
2.5 全局匹配g
1 2 3
let repeatRegex = /Repeat/g; testStr.match(repeatRegex); // Returns ["Repeat", "Repeat", "Repeat"]
2.6 通配符.
1 2 3 4
let exampleStr = "Let's have fun with regular expressions!"; let unRegex = /.un/; let result = unRegex.test(exampleStr); // it matches the strings "run", "sun", "fun", "pun", "nun", and "bun".
2.7[]
查找方括号之间的任何字符。
1 2 3 4 5 6 7 8 9
let bigStr = "big"; let bagStr = "bag"; let bugStr = "bug"; let bogStr = "bog"; let bgRegex = /b[aiu]g/; bigStr.match(bgRegex); // Returns ["big"] bagStr.match(bgRegex); // Returns ["bag"] bugStr.match(bgRegex); // Returns ["bug"] bogStr.match(bgRegex); // Returns null
使用-
1 2 3 4 5 6 7
let catStr = "cat"; let batStr = "bat"; let matStr = "mat"; let bgRegex = /[a-e]at/; catStr.match(bgRegex); // Returns ["cat"] batStr.match(bgRegex); // Returns ["bat"] matStr.match(bgRegex); // Returns null
匹配数字和字母
1 2 3 4
let jennyStr = "Jenny8675309"; let myRegex = /[a-z0-9]/ig; // matches all letters and numbers in jennyStr jennyStr.match(myRegex);
2.8 使用^匹配非
[^abc]查找任何不在方括号之间的字符。
例子:匹配不是数字也不是元音的字符
1 2 3
let quoteSample = "3 blind mice."; let myRegex = /[^0-9aeiou]/gi; let result = quoteSample.match(myRegex);
2.9 量词
用+匹配一次或多次
1 2 3 4 5
let difficultSpelling = "Mississippi"; let myRegex = /s+/g; let result = difficultSpelling.match(myRegex); console.log(result); // ["ss", "ss"]
用*匹配0次或多次
1 2 3 4 5 6 7
let soccerWord = "gooooooooal!"; let gPhrase = "gut feeling"; let oPhrase = "over the moon"; let goRegex = /go*/; soccerWord.match(goRegex); // Returns ["goooooooo"] gPhrase.match(goRegex); // Returns ["g"] oPhrase.match(goRegex); // Returns null
用?匹配0次或1次
1 2 3 4 5
let american = "color"; let british = "colour"; let rainbowRegex= /colou?r/; rainbowRegex.test(american); // Returns true rainbowRegex.test(british); // Returns true
2.10 惰性匹配
1 2 3 4 5
let str = "<h1>Winter is coming</h1>"; let myRegex = /<.*?>/; let result = str.match(myRegex); console.log(result); // ["<h1>"]
2.11 匹配字符串的开始和结尾
用^匹配开始
1 2 3
let rickyAndCal = "Cal and Ricky both like racing."; let calRegex = /^Cal/; let result = calRegex.test(rickyAndCal);
用$匹配结尾
1 2 3
let caboose = "The last car on a train is the caboose"; let lastRegex = /caboose$/; let result = lastRegex.test(caboose);
2.12 匹配所有字母、数字、下划线 \w
1 2 3 4 5 6 7 8
let longHand = /[A-Za-z0-9_]+/; let shortHand = /\w+/; let numbers = "42"; let varNames = "important_var"; longHand.test(numbers); // Returns true shortHand.test(numbers); // Returns true longHand.test(varNames); // Returns true shortHand.test(varNames); // Returns true
2.13 匹配非字母、数字、下划线 \W
1 2 3 4 5
let shortHand = /\W/; let numbers = "42%"; let sentence = "Coding!"; numbers.match(shortHand); // Returns ["%"] sentence.match(shortHand); // Returns ["!"]
2.14 匹配所有数字 \d
2.15 匹配非数字 \D
2.16 匹配空白\s
匹配任何空白字符,包括空格、制表符、换页符等等。等价于[ \f\n\r\t\v]。
2.17 匹配非空白 \S
匹配任何非空白字符。等价于[^\f\n\r\t\v]。
2.18 用{}指定匹配数
用{3,5}表示匹配3到5次
1 2 3 4 5
let A4 = "aaaah"; let A2 = "aah"; let multipleA = /a{3,5}h/; multipleA.test(A4); // Returns true multipleA.test(A2); // Returns false
用{3,}表示至少匹配3次
1 2 3 4 5 6 7
let A4 = "haaaah"; let A2 = "haah"; let A100 = "h" + "a".repeat(100) + "h"; let multipleA = /ha{3,}h/; multipleA.test(A4); // Returns true multipleA.test(A2); // Returns false multipleA.test(A100); // Returns true
用{3}表示匹配确定的3次
1 2 3 4 5 6 7
let A4 = "haaaah"; let A3 = "haaah"; let A100 = "h" + "a".repeat(100) + "h"; let multipleHA = /ha{3}h/; multipleHA.test(A4); // Returns false multipleHA.test(A3); // Returns true multipleHA.test(A100); // Returns false