JavaScript正則表達字典

測試正則表達式

  • 使用.test()方法
let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);
複製代碼
  • 測試多項匹配使用OR運算符(|)
let testString = "My test string";
const regex = /test|My|string/;
regex.test(testString);
複製代碼
  • 使用(i)忽略大小寫
let testString = "My test string";
const ignoreReg = /my/i;
ignoreReg.test(testString);
複製代碼

獲取匹配到的內容

  • 獲取第一個匹配
const match = "My test string".match(/test/i);
複製代碼
  • 獲取全部匹配使用(g)
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"]
複製代碼
  • 匹配字母使用字符集範圍[a-z]
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", "!"]
複製代碼
  • 匹配連續出現1次或屢次的字符,使用符號(+)
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"]
複製代碼
  • 匹配連續出現0次或屢次的字符,使用符號(*)
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
複製代碼
  • 匹配全部字母和數字,使用符號 word(\w)
var shortHand = /\w+/
var numbers = "0123456789";
var str = "abcdefghijklmn";
var rS = numbers.match(shortHand);  //"0123456789"
var rN = str.match(shortHand);      //"abcdefghijklmn"
複製代碼
  • 匹配除了字母和數字的全部內容,使用符號(\W)
var word = "123qwe";
var noWord = "!@#@#%#$";
var regex = /\W+/;
var rw = word.match(regex);         //null
var rwN = noWord.match(regex);      //["!@#@#%#$"]
複製代碼
  • 匹配全部數字,使用符號 digest(\d)
var str = "i am 23 years old";
var regex = /\d+/;
var result = str.match(regex)       //["23"]
複製代碼
  • 匹配全部非數字,使用符號(\D)
var str = "i am 23 years old";
var regex = /\D+/;
var result = str.match(regex)       //["i am "]
複製代碼
  • 匹配空格,使用符號(\s)
var str = "How are you?";
var regex = /\s+/g;
var result = str.match(regex)       //[" ", " "]
複製代碼
  • 匹配非空格,使用符號(\S)
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
複製代碼
相關文章
相關標籤/搜索