JS 常用正则表达式

论坛 期权论坛 期权     
一个优秀的废人   2019-7-15 09:30   5135   0
点击上方“一个优秀的废人”,选择“置顶或者星标”
你关注的就是我关心的!

作者:前端小智
https://segmentfault.com/a/1190000018963103
1、匹配正则
使用
  1. .test()
复制代码
方法

  1. let testString = "My test string";
复制代码
  1. let testRegex = /string/;
复制代码
  1. testRegex.test(testString);
复制代码

[h1]2、匹配多个模式[/h1]使用操作符号
  1. |
复制代码

  1. const regex = /yes|no|maybe/;
复制代码
[h1]3、忽略大小写[/h1]使用
  1. i
复制代码
标志表示忽略大小写

  1. const caseInsensitiveRegex = /ignore case/i;
复制代码
  1. const testString = 'We use the i flag to iGnOrE CasE';
复制代码
  1. caseInsensitiveRegex.test(testString); // true
复制代码

[h1]4、提取变量的第一个匹配项[/h1]使用
  1. .match()
复制代码
方法

  1. const match = "Hello World!".match(/hello/i); // "Hello"
复制代码

[h1]5、提取数组中的所有匹配项[/h1]使用
  1. g
复制代码
标志

  1. const testString = "Repeat repeat rePeAT";
复制代码
  1. const regexWithAllMatches = /Repeat/gi;
复制代码
  1. testString.match(regexWithAllMatches); // ["Repeat", "repeat", "rePeAT"]
复制代码

[h1]6、匹配任意字符[/h1]使用通配符
  1. .
复制代码
作为任何字符的占位符

  1. // To match "cat", "BAT", "fAT", "mat"
复制代码
  1. const regexWithWildcard = /.at/gi;
复制代码
  1. const testString = "cat BAT cupcake fAT mat dog";
复制代码
  1. const allMatchingWords = testString.match(regexWithWildcard); // ["cat", "BAT", "fAT", "mat"]   
复制代码
  1. 用多种可能性匹配单个字符
复制代码
  • 使用字符类,你可以使用它来定义要匹配的一组字符
  • 把它们放在方括号里
    1. []
    复制代码

  1. //匹配 "cat" "fat" and "mat" 但不匹配 "bat"
复制代码
  1. const regexWithCharClass = /[cfm]at/g;
复制代码
  1. const testString = "cat fat bat mat";
复制代码
  1. const allMatchingWords = testString.match(regexWithCharClass); // ["cat", "fat", "mat"]
复制代码
[h1]7、匹配字母表中的字母[/h1]使用字符集内的范围
  1. [a-z]
复制代码

  1. const regexWidthCharRange = /[a-e]at/;
复制代码
  1. [/code][code]const regexWithCharRange = /[a-e]at/;
复制代码
  1. const catString = "cat";
复制代码
  1. const batString = "bat";
复制代码
  1. const fatString = "fat";
复制代码
  1. [/code][code]regexWithCharRange.test(catString); // true
复制代码
  1. regexWithCharRange.test(batString); // true
复制代码
  1. regexWithCharRange.test(fatString); // false
复制代码
[h1]8、匹配特定的数字和字母[/h1]你还可以使用连字符来匹配数字

  1. const regexWithLetterAndNumberRange = /[a-z0-9]/ig;
复制代码
  1. const testString = "Emma19382";
复制代码
  1. testString.match(regexWithLetterAndNumberRange) // true
复制代码

[h1]9、匹配单个未知字符[/h1]要匹配您不想拥有的一组字符,使用否定字符集
  1. ^
复制代码

  1. const allCharsNotVowels = /[^aeiou]/gi;
复制代码
  1. const allCharsNotVowelsOrNumbers = /[^aeiou0-9]/gi;
复制代码
[h1]10、匹配一行中出现一次或多次的字符[/h1]使用
  1. +
复制代码
标志

  1. const oneOrMoreAsRegex = /a+/gi;
复制代码
  1. const oneOrMoreSsRegex = /s+/gi;
复制代码
  1. const cityInFlorida = "Tallahassee";
复制代码
  1. [/code][code]cityInFlorida.match(oneOrMoreAsRegex); // ['a', 'a', 'a'];
复制代码
  1. cityInFlorida.match(oneOrMoreSsRegex); // ['ss'];   
复制代码

[h1]11、匹配连续出现零次或多次的字符[/h1]使用星号
  1. *
复制代码

  1. const zeroOrMoreOsRegex = /hi*/gi;
复制代码
  1. const normalHi = "hi";
复制代码
  1. const happyHi = "hiiiiii";
复制代码
  1. const twoHis = "hiihii";
复制代码
  1. const bye = "bye";
复制代码
  1. [/code][code]normalHi.match(zeroOrMoreOsRegex); // ["hi"]
复制代码
  1. happyHi.match(zeroOrMoreOsRegex); // ["hiiiiii"]
复制代码
  1. twoHis.match(zeroOrMoreOsRegex); // ["hii", "hii"]
复制代码
  1. bye.match(zeroOrMoreOsRegex); // null
复制代码
  1. [/code]
  2. [h1][i][b]12、[/b][/i][b]惰性匹配[/b][/h1][list][*]字符串中与给定要求匹配的最小部分
  3. [*]默认情况下,正则表达式是贪婪的(匹配满足给定要求的字符串的最长部分)
  4. [*]使用 [code]?
复制代码
阻止贪婪模式(惰性匹配 )

  1.   const testString = "catastrophe";
复制代码
  1.     const greedyRexex = /c[a-z]*t/gi;
复制代码
  1.     const lazyRegex = /c[a-z]*?t/gi;
复制代码
  1.    
复制代码
  1.     testString.match(greedyRexex); // ["catast"]
复制代码
  1.     testString.match(lazyRegex); // ["cat"]
复制代码
[h1]13、匹配起始字符串模式[/h1]要测试字符串开头的字符匹配,请使用插入符号
  1. ^
复制代码
,但要放大开头,不要放到字符集中

  1. const emmaAtFrontOfString = "Emma likes cats a lot.";
复制代码
  1. const emmaNotAtFrontOfString = "The cats Emma likes are fluffy.";
复制代码
  1. const startingStringRegex = /^Emma/;
复制代码
  1. [/code][code]startingStringRegex.test(emmaAtFrontOfString); // true
复制代码
  1. startingStringRegex.test(emmaNotAtFrontOfString); // false   
复制代码
  1.    
复制代码

[h1]14、匹配结束字符串模式[/h1]使用
  1. $
复制代码
来判断字符串是否是以规定的字符结尾

  1. const emmaAtBackOfString = "The cats do not like Emma";
复制代码
  1. const emmaNotAtBackOfString = "Emma loves the cats";
复制代码
  1. const startingStringRegex = /Emma$/;
复制代码
  1. [/code][code]startingStringRegex.test(emmaAtBackOfString); // true
复制代码
  1. startingStringRegex.test(emmaNotAtBackOfString); // false
复制代码

[h1]15、匹配所有字母和数字[/h1]使用
  1. \word
复制代码
简写

  1. const longHand = /[A-Za-z0-9_]+/;
复制代码
  1. const shortHand = /\w+/;
复制代码
  1. const numbers = "42";
复制代码
  1. const myFavoriteColor = "magenta";
复制代码
  1. [/code][code]longHand.test(numbers); // true
复制代码
  1. shortHand.test(numbers); // true
复制代码
  1. longHand.test(myFavoriteColor); // true
复制代码
  1. shortHand.test(myFavoriteColor); // true
复制代码

[h1]16、除了字母和数字,其他的都要匹配[/h1]用
  1. \W
复制代码
表示
  1. \w
复制代码
的反义

  1. const noAlphaNumericCharRegex = /\W/gi;
复制代码
  1. const weirdCharacters = "!_$!!";
复制代码
  1. const alphaNumericCharacters = "ab283AD";
复制代码
  1. [/code][code]noAlphaNumericCharRegex.test(weirdCharacters); // true
复制代码
  1. noAlphaNumericCharRegex.test(alphaNumericCharacters); // false
复制代码
  1. [/code]
  2. [h1][i][b]17、[/b][/i][b]匹配所有数字[/b][/h1]你可以使用字符集[code][0-9]
复制代码
,或者使用简写
  1. \d
复制代码

  1. const digitsRegex = /\d/g;
复制代码
  1. const stringWithDigits = "My cat eats $20.00 worth of food a week.";
复制代码
  1. [/code][code]stringWithDigits.match(digitsRegex); // ["2", "0", "0", "0"]
复制代码
  1. [/code]
  2. [h1][i][b]18、[/b][/i][b]匹配所有非数字[/b][/h1]用[code]\D
复制代码
表示
  1. \d
复制代码
的反义

  1. const nonDigitsRegex = /\D/g;
复制代码
  1. const stringWithLetters = "101 degrees";
复制代码
  1. [/code][code]stringWithLetters.match(nonDigitsRegex); // [" ", "d", "e", "g", "r", "e", "e", "s"]
复制代码

[h1]19、匹配空格[/h1]使用
  1. \s
复制代码
来匹配空格和回车符

  1. const sentenceWithWhitespace = "I like cats!"
复制代码
  1. var spaceRegex = /\s/g;
复制代码
  1. whiteSpace.match(sentenceWithWhitespace); // [" ", " "]
复制代码

[h1]20、匹配非空格[/h1]用
  1. \S
复制代码
表示
  1. \s
复制代码
的反义

  1. const sentenceWithWhitespace = "C a t"
复制代码
  1. const nonWhiteSpaceRegex = /\S/g;
复制代码
  1. sentenceWithWhitespace.match(nonWhiteSpaceRegex); // ["C", "a", "t"]
复制代码
  1. [/code]
  2. [h1][i][b]21、[/b][/i][b]匹配的字符数[/b][/h1]你可以使用 [code]{下界,上界}
复制代码
指定一行中的特定字符数

  1. const regularHi = "hi";
复制代码
  1. const mediocreHi = "hiii";
复制代码
  1. const superExcitedHey = "heeeeyyyyy!!!";
复制代码
  1. const excitedRegex = /hi{1,4}/;
复制代码
  1. [/code][code]excitedRegex.test(regularHi); // true
复制代码
  1. excitedRegex.test(mediocreHi); // true
复制代码
  1. excitedRegex.test(superExcitedHey); //false
复制代码
[h1]22、匹配最低个数的字符数[/h1]使用
  1. {下界, }
复制代码
定义最少数量的字符要求,下面示例表示字母
  1. i
复制代码
至少要出现2次

  1. const regularHi = "hi";
复制代码
  1. const mediocreHi = "hiii";
复制代码
  1. const superExcitedHey = "heeeeyyyyy!!!";
复制代码
  1. const excitedRegex = /hi{2,}/;
复制代码
  1. [/code][code]excitedRegex.test(regularHi); // false
复制代码
  1. excitedRegex.test(mediocreHi); // true
复制代码
  1. excitedRegex.test(superExcitedHey); //false
复制代码

[h1]23、匹配精确的字符数[/h1]使用
  1. {requiredCount}
复制代码
指定字符要求的确切数量

  1. const regularHi = "hi";
复制代码
  1. const bestHi = "hii";
复制代码
  1. const mediocreHi = "hiii";
复制代码
  1. const excitedRegex = /hi{2}/;
复制代码
  1. [/code][code]excitedRegex.test(regularHi); // false
复制代码
  1. excitedRegex.test(bestHi); // true
复制代码
  1. excitedRegex.test(mediocreHi); //false
复制代码
[h1]24、匹配0次或1次[/h1]使用
  1. ?
复制代码
匹配字符 0 次或1次

  1. const britishSpelling = "colour";
复制代码
  1. const americanSpelling = "Color";
复制代码
  1. const languageRegex = /colou?r/i;
复制代码
  1. [/code][code]languageRegex.test(britishSpelling); // true
复制代码
  1. languageRegex.test(americanSpelling); // true
复制代码
最近热文阅读:
1、java | 什么是动态代理
2、实用工具 | 推荐 9 个chrome 扩展
3、springboot | 自动配置原理

好文章,我在看
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:5
帖子:1
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP