測試正則表達式
let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);
複製代碼
let testString = "My test string";
const regex = /test|My|string/;
regex.test(testString);
複製代碼
let testString = "My test string";
const ignoreReg = /my/i;
ignoreReg.test(testString);
複製代碼
獲取匹配到的內容
const match = "My test string".match(/test/i);
複製代碼
const match = "My test string,My regex will be better!".match(/my/ig);
複製代碼
const regexWithWildcard = /.at/gi;
const testString = "cat BAT cupcake AT mat dog";
const allMatchingWords = testString.match(regexWithWildcard);
//["cat", "BAT", " AT", "mat"]
複製代碼
- 匹配具備多種可能性的單個字符,把它們放在([])中
const regexWithCharClass = /[cfm]at/g;
const testString = "cat fat bat mat";
const allMatchingWords = testString.match(regexWithCharClass);
//["cat", "fat", "mat"]
複製代碼
var regexWithCharRange = /[a-e]at/;
var catString = "cat";
var batString = "bat";
var fatString = "fat";
var cStr = catString.match(regexWithCharRange)
//["cat"]
var fStr = fatString.match(regexWithCharRange)
//null
複製代碼
var str = "hello, i am meteor, i am 12years old";
var regex = /[0-9a-z]/ig;
var rStr = regex.test(str);
複製代碼
const regex = /[^aeiou]/gi;
var str = "how a beatuiful day it is!";
var match = str.match(regex);
//["h", "w", " ", " ", "b", "t", "f", "l", " ", "d", "y", " ", "t", " ", "s", "!"]
複製代碼
var regL = /l+/gi;
var regH = /h+/gi;
var str = "hello!how are you?";
var r1 = str.match(regL) //["ll"]
var r2 = str.match(regH) //["h", "h"]
複製代碼
var regex = /hi*/gi;
var normalHi = "Hi";
var happyHi = "hiiiiii";
var twoHi = "hiihii";
var normalResult = normalHi.match(regex); //["Hi"]
var happyResult = happyHi.match(regex); //["hiiiiii"]
var twoResult = twoHi.match(regex); //["hii", "hii"]
複製代碼
var str = "catastrophe";
var regex = /c[a-z]*t/;
var regexLazy = /c[a-z]*?t/;
var result = str.match(regex) //["catast"]
var resultLazy = str.match(regexLazy) //["cat"]
複製代碼
var str1 = "how are you";
var str2 = "you are welcome";
var regex = /^you/ig;
var r1 = str1.match(regex) //null
var r2 = str2.match(regex) //["you"]
複製代碼
var str1 = "how are you";
var str2 = "you are welcome";
var regex = /you$/ig;
var r1 = str1.match(regex) //["you"]
var r2 = str2.match(regex) //null
複製代碼
var shortHand = /\w+/
var numbers = "0123456789";
var str = "abcdefghijklmn";
var rS = numbers.match(shortHand); //"0123456789"
var rN = str.match(shortHand); //"abcdefghijklmn"
複製代碼
var word = "123qwe";
var noWord = "!@#@#%#$";
var regex = /\W+/;
var rw = word.match(regex); //null
var rwN = noWord.match(regex); //["!@#@#%#$"]
複製代碼
var str = "i am 23 years old";
var regex = /\d+/;
var result = str.match(regex) //["23"]
複製代碼
var str = "i am 23 years old";
var regex = /\D+/;
var result = str.match(regex) //["i am "]
複製代碼
var str = "How are you?";
var regex = /\s+/g;
var result = str.match(regex) //[" ", " "]
複製代碼
var str = "How are you?";
var regex = /\S+/g;
var result = str.match(regex); //["How", "are", "you?"]
複製代碼
- 匹配字符數,匹配字符串中特定的字符數,使用符號({}) {lowerBound, upperBound}
var regularHi = "hi";
var mediocreHi = "hiii";
var superExcitedHey = "heeeeyyyyy!!!";
var excitedRegex = /hi{1,4}/;
var resultRegular = regularHi.match(excitedRegex); // ["hi"]
var resultMediocre = mediocreHi.match(excitedRegex); // ["hiii"]
var resultSuper = superExcitedHey.match(excitedRegex); // null
複製代碼
- 匹配至少字符數,使用({}) {lowerBound,}
var regularHi = "hi";
var mediocreHi = "hiii";
var superExcitedHey = "heeeeyyyyy!!!";
var excitedRegex = /hi{1,}/;
var resultRegular = regularHi.match(excitedRegex); // ["hi"]
var resultMediocre = mediocreHi.match(excitedRegex); // ["hiii"]
var resultSuper = superExcitedHey.match(excitedRegex); // null
複製代碼
- 匹配確切字符數,使用({}) {requiredCount}
const regularHi = "hi";
const bestHi = "hii";
const mediocreHi = "hiii";
const excitedRegex = /hi{2}/;
excitedRegex.test(regularHi); // false
excitedRegex.test(bestHi); // true
excitedRegex.test(mediocreHi); //false
複製代碼