当前位置: 移动技术网 > IT编程>开发语言>正则 > JavaScript 正则表达式备忘单实例代码

JavaScript 正则表达式备忘单实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论

正则表达式或“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

总结

以上所述是小编给大家介绍的javascript  正则表达式备忘单实例代码,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网