JS经常使用正则表达式备忘录

想浏览更多优良文章请猛戳GitHub博客,一年百来篇优良文章等着你!

正则表达式或“regex”用于婚配字符串的各个部份 下面是我建立正则表达式的备忘单。

婚配正则

运用 .test() 要领

let testString = "My test string";
let testRegex = /string/;
testRegex.test(testString);

婚配多个形式

运用操纵标记 |

const regex = /yes|no|maybe/;    

疏忽大小写

运用i标志示意疏忽大小写

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

提取变量的第一个婚配项

运用 .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";

regexWithCharRange.test(catString); // true
regexWithCharRange.test(batString); // true
regexWithCharRange.test(fatString); // false

婚配特定的数字和字母

你还能够运用连字符来婚配数字

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/;

startingStringRegex.test(emmaAtFrontOfString); // true
startingStringRegex.test(emmaNotAtFrontOfString); // false    
    

婚配完毕字符串形式

运用 $ 来推断字符串是不是是以划定的字符末端

const emmaAtBackOfString = "The cats do not like Emma";
const emmaNotAtBackOfString = "Emma loves the cats";
const startingStringRegex = /Emma$/;

startingStringRegex.test(emmaAtBackOfString); // true
startingStringRegex.test(emmaNotAtBackOfString); // false    

婚配一切字母和数字

运用\word 简写

const longHand = /[A-Za-z0-9_]+/;
const shortHand = /\w+/;
const numbers = "42";
const myFavoriteColor = "magenta";

longHand.test(numbers); // true
shortHand.test(numbers); // true
longHand.test(myFavoriteColor); // true
shortHand.test(myFavoriteColor); // true

除了字母和数字,其他的都要婚配

\W 示意 \w 的反义

const noAlphaNumericCharRegex = /\W/gi;
const weirdCharacters = "!_$!!";
const alphaNumericCharacters = "ab283AD";

noAlphaNumericCharRegex.test(weirdCharacters); // true
noAlphaNumericCharRegex.test(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}/;

excitedRegex.test(regularHi); // true
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false

婚配最低个数的字符数

运用{下界, }定义起码数目的字符请求,下面示例示意字母 i 最少要涌现2次

const regularHi = "hi";
const mediocreHi = "hiii";
const superExcitedHey = "heeeeyyyyy!!!";
const excitedRegex = /hi{2,}/;

excitedRegex.test(regularHi); // false
excitedRegex.test(mediocreHi); // true
excitedRegex.test(superExcitedHey); //false

婚配准确的字符数

运用{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
    

婚配0次或1次

运用 ? 婚配字符 0 次或1次

const britishSpelling = "colour";
const americanSpelling = "Color";
const languageRegex = /colou?r/i;

languageRegex.test(britishSpelling); // true
languageRegex.test(americanSpelling); // true

代码布置后能够存在的BUG没法及时晓得,预先为了处理这些BUG,花了大批的时候举行log 调试,这边顺便给人人引荐一个好用的BUG监控东西 Fundebug

你的点赞是我延续分享好东西的动力,迎接点赞!

交换

干货系列文章汇总以下,以为不错点个Star,迎接 加群 互相进修。

https://github.com/qq44924588…

我是小智,民众号「大迁天下」作者,对前端手艺坚持进修爱好者。我会常常分享本身所学所看的干货,在进阶的路上,共勉!

关注民众号,背景复兴福利,即可看到福利,你懂的。

《JS经常使用正则表达式备忘录》

    原文作者:前端小智
    原文地址: https://segmentfault.com/a/1190000018963103
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞