js正则表达式验证、匹配数字、匹配字符串、匹配中⽂、匹配任
意字符备忘录
正则表达式或“regex”⽤于匹配字符串的各个部分,下⾯是我创建正则表达式的备忘录。包括⼀些常⽤的验证、匹配数字、匹配字符串、匹配中⽂、匹配任意字符串。
匹配正则
使⽤ .test() ⽅法
let testString = "My test string";
let testRegex = /string/;
匹配多个模式
使⽤操作符号 |
const regex = /yes|no|maybe/;
忽略⼤⼩写
使⽤i标志表⽰忽略⼤⼩写
const caseInsensitiveRegex = /ignore case/i;
const testString = 'We use the i flag to iGnOrE CasE';
正则表达式提取中文字符提取变量的第⼀个匹配项
使⽤ .match() ⽅法
const match = "Hello World!".match(/hello/i); // "Hello"
提取数组中的所有匹配项
使⽤ g 标志
const testString = "Repeat repeat rePeAT";
const regexWithAllMatches = /Repeat/gi;
testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]
匹配任意字符
使⽤通配符. 作为任何字符的占位符
// To match "cat", "BAT", "fAT", "mat"
const regexWithWildcard = /.at/gi;
const testString = "cat BAT cupcake fAT mat dog";
const allMatchingWords = testString.match(regexWithWildcard); // ["cat", "BAT", "fAT", "mat"]
⽤多种可能性匹配单个字符
使⽤字符类,你可以使⽤它来定义要匹配的⼀组字符把它们放在⽅括号⾥ []
/
/匹配 "cat" "fat" and "mat" 但不匹配 "bat"
const regexWithCharClass = /[cfm]at/g;
const testString = "cat fat bat mat";
const allMatchingWords = testString.match(regexWithCharClass); // ["cat", "fat", "mat"]
匹配字母表中的字母
使⽤字符集内的范围 [a-z]
const regexWidthCharRange = /[a-e]at/;
const regexWithCharRange = /[a-e]at/;
const catString = "cat";
const batString = "bat";
const fatString = "fat";
匹配特定的数字和字母
你还可以使⽤连字符来匹配数字
const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
const testString = "Emma19382";
testString.match(regexWithLetterAndNumberRange) // true
匹配单个未知字符
要匹配您不想拥有的⼀组字符,使⽤否定字符集 ^
const allCharsNotVowels = /[^aeiou]/gi;
const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;
匹配⼀⾏中出现⼀次或多次的字符
使⽤ + 标志
const oneOrMoreAsRegex = /a+/gi;
const oneOrMoreSsRegex = /s+/gi;
const cityInFlorida = "Tallahassee";
cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];
匹配连续出现零次或多次的字符
使⽤星号 *
const zeroOrMoreOsRegex = /hi*/gi;
const normalHi = "hi";
const happyHi = "hiiiiii";
const twoHis = "hiihii";
const bye = "bye";
normalHi.match(zeroOrMoreOsRegex); // ["hi"]
happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
bye.match(zeroOrMoreOsRegex); // null
惰性匹配
字符串中与给定要求匹配的最⼩部分
默认情况下,正则表达式是贪婪的(匹配满⾜给定要求的字符串的最长部分)
使⽤ ? 阻⽌贪婪模式(惰性匹配 )
const testString = "catastrophe";
const greedyRexex = /c[a-z]*t/gi;
const lazyRegex = /c[a-z]*?t/gi;
testString.match(greedyRexex); // ["catast"]
testString.match(lazyRegex); // ["cat"]
匹配起始字符串模式
要测试字符串开头的字符匹配,请使⽤插⼊符号^,但要放⼤开头,不要放到字符集中const emmaAtFrontOfString = "Emma likes cats a lot.";
const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
const startingStringRegex = /^Emma/;
匹配结束字符串模式
使⽤ $ 来判断字符串是否是以规定的字符结尾
const emmaAtBackOfString = "The cats do not like Emma";
const emmaNotAtBackOfString = "Emma loves the cats";
const startingStringRegex = /Emma$/;
匹配所有字母和数字
使⽤\word 简写
const longHand = /[A-Za-z0-9_]+/;
const shortHand = /\w+/;
const numbers = "42";
const myFavoriteColor = "magenta";
除了字母和数字,其他的都要匹配
⽤\W 表⽰ \w 的反义
const noAlphaNumericCharRegex = /\W/gi;
const weirdCharacters = "!_$!!";
const alphaNumericCharacters = "ab283AD"; st(weirdCharacters); // true st(alphaNumericCharacters); // false
匹配所有数字
你可以使⽤字符集[0-9],或者使⽤简写 \d
const digitsRegex = /\d/g;
const stringWithDigits = "My cat eats $20.00 worth of food a week."; stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]
匹配所有⾮数字
⽤\D 表⽰ \d 的反义
const nonDigitsRegex = /\D/g;
const stringWithLetters = "101 degrees";
stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]
匹配空格
使⽤ \s 来匹配空格和回车符
const sentenceWithWhitespace = "I like cats!"
var spaceRegex = /\s/g;
whiteSpace.match(sentenceWithWhitespace); // [" ", " "]
匹配⾮空格
⽤\S 表⽰ \s 的反义
const sentenceWithWhitespace = "C a t"
const nonWhiteSpaceRegex = /\S/g;
sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]
匹配的字符数
你可以使⽤ {下界,上界} 指定⼀⾏中的特定字符数
const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy";
const excitedRegex = /hi{1,4}/;
匹配最低个数的字符数
使⽤{下界, }定义最少数量的字符要求,下⾯⽰例表⽰字母 i ⾄少要出现2次const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy";
const excitedRegex = /hi{2,}/;
匹配精确的字符数
使⽤{requiredCount}指定字符要求的确切数量
const regularHi = "hi";
const bestHi = "hii";
const mediocreHi = "hiii";
const excitedRegex = /hi{2}/;
匹配0次或1次
使⽤ ? 匹配字符 0 次或1次
const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou?r/i; st(britishSpelling); // true st(americanSpelling); // true
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论