You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// To match "cat", "BAT", "fAT", "mat"constregexWithWildcard=/.at/gi;consttestString="cat BAT cupcake fAT mat dog";constallMatchingWords=testString.match(regexWithWildcard);// ["cat", "BAT", "fAT", "mat"]
匹配具有多种可能性的单个字符
使用字符类,您可以使用它来定义要匹配的一组字符
你把它们放在方括号内 []
// Match "cat" "fat" and "mat" but not "bat"constregexWithCharClass=/[cfm]at/g;consttestString="cat fat bat mat";constallMatchingWords=testString.match(regexWithCharClass);// ["cat", "fat", "mat"]
constemmaAtFrontOfString="Emma likes cats a lot.";constemmaNotAtFrontOfString="The cats Emma likes are fluffy.";conststartingStringRegex=/^Emma/;startingStringRegex.test(emmaAtFrontOfString);// truestartingStringRegex.test(emmaNotAtFrontOfString);// false
匹配结束字符串模式
使用$正则表达式末尾的美元符号来检查字符串末尾是否存在
constemmaAtBackOfString="The cats do not like Emma";constemmaNotAtBackOfString="Emma loves the cats";conststartingStringRegex=/Emma$/;startingStringRegex.test(emmaAtBackOfString);// truestartingStringRegex.test(emmaNotAtBackOfString);// false
正则表达式或
regex
用于匹配字符串的各个部分。下面是创建的正则表达式的备忘单。测试正则表达式
测试多项匹配
const regex = /yes|no|maybe/;
忽略大小写
将第一个匹配提取到变量
const match = "Hello World!".match(/hello/i); // "Hello"
提取数组中的所有匹配项
匹配任何字符
匹配具有多种可能性的单个字符
匹配字母表的字母
匹配特定的数字和字母
匹配单个未知字符
匹配连续出现一次或多次的字符
匹配连续出现零次或多次的字符
懒惰匹配
匹配起始字符串模式
匹配结束字符串模式
匹配所有字母和数字
除了字母和数字之外的所有内容
匹配所有数字
匹配所有非数字
匹配空格
匹配非空格
匹配字符数
匹配最少的字符数
匹配确切数量的字符数
匹配全部或不匹配的字符
The text was updated successfully, but these errors were encountered: