正则表达式(regex/regexp)

本文详细介绍了如何使用正则表达式进行字符串匹配、大小写敏感与不敏感、提取特定模式、全局搜索、通配符和字符类的应用。通过实例演示了如何在JavaScript中利用正则表达式解决实际问题,如匹配多种可能性、提取单词等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、使用test方法,返回true、false
大小写有区分

let testStr = "freeCodeCamp";
let testRegex = /Code/;
testRegex.test(testStr);

2、匹配具有不同可能性的文字字符串

let petString = "James has a pet cat.";
let petRegex = /dog|cat|bird|fish/; // Change this line
let result = petRegex.test(petString);

3、匹配时忽略大小写

let myString = "freeCodeCamp";
let fccRegex = /freeCodeCamp/i; // Change this line
let result = fccRegex.test(myString);

4、提取匹配项

let extractStr = "Extract the word 'coding' from this string.";
let codingRegex = /coding/; // Change this line
let result = extractStr.match(codingRegex); // Change this line

5、Find More Than the First Match
要多次搜索或提取模式,您可以使用全局搜索标志:g. 可以在你的正则表达式上有多个标志,比如/search/gi

let twinkleStar = "Twinkle, twinkle, little star";
let starRegex = /Twinkle/gi; // Change this line
let result = twinkleStar.match(starRegex); // Change this line

6、匹配通配符期间的任何内容
通配符.将匹配任何一个字符。可以像使用正则表达式中的任何其他字符一样使用通配符。例如想匹配hug、huh、hut和hum,可以使用正则表达式/hu./来匹配所有四个单词。

let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /.un/; // Change this line
let result = unRegex.test(exampleStr);

7、匹配具有多种可能性的单个字符
想匹配bag,big和bug但不匹配bog。您可以创建正则表达式/b[aiu]g/来执行此操作。[aiu]是仅匹配字符a、i或的字符类u。

let quoteSample = "Beware of bugs in the above code; I have only proved it correct, not tried it.";
let vowelRegex = /[aeiou]/gi; // Change this line
let result = quoteSample.match(vowelRegex); // Change this line

8、匹配字母
需要匹配大范围的字符,例如,要匹配小写字母a,e您将使用[a-e].

let quoteSample = "The quick brown fox jumps over the lazy dog.";
let alphabetRegex = /[a-z]/gi; // Change this line
let result = quoteSample.match(alphabetRegex); // Change this line

9、匹配未指定的单个字符
创建否定字符集,请在左^括号之后和不想匹配的字符之前放置一个脱字符 ( )。例如,/[^aeiou]/gi匹配所有不是元音的字符。

let quoteSample = "3 blind mice.";
let myRegex = /[^aeiou0-9]/gi; // Change this line
let result = quoteSample.match(myRegex); // Change this line

10、匹配出现一次或多次的字符
例如,/a+/g将在其中找到一个匹配项abc并返回[“a”]。由于+,它也会在 中找到一个匹配项aabc并返回[“aa”]。

let difficultSpelling = "Mississippi";
let myRegex = /s+/gi; // Change this line
let result = difficultSpelling.match(myRegex);

11、匹配出现零次或多次的字符

// Only change code below this line
let chewieRegex = /Aa*/g; // Change this line
// Only change code above this line
let result = chewieQuote.match(chewieRegex);

12、使用惰性匹配查找字符
修复正则表达式/<.*>/以返回 HTML 标记

而不是 text “

Winter is coming

”。记住.正则表达式中的通配符匹配任何字符。

let text = "<h1>Winter is coming</h1>";
let myRegex = /<[a-z0-9]*?>/; // Change this line
let result = text.match(myRegex);

13、匹配起始字符串模式

let rickyAndCal = "Cal and Ricky both like racing.";
let calRegex = /^Cal/; // Change this line
let result = calRegex.test(rickyAndCal);

14、匹配结束字符串模式

let caboose = "The last car on a train is the caboose";
let lastRegex = /caboose$/; // Change this line
let result = lastRegex.test(caboose);

15、匹配所有字母和数字
使用速记字符类\w计算各种引号和字符串中的字母数字字符的数量。

let quoteSample = "The five boxing wizards jump quickly.";
let alphabetRegexV2 = /\w/g; // Change this line
let result = quoteSample.match(alphabetRegexV2).length;

16、匹配除字母和数字之外的所有内容 \W

let quoteSample = "The five boxing wizards jump quickly.";
let nonAlphabetRegex = /\W/g; // Change this line
let result = quoteSample.match(nonAlphabetRegex).length;

17、匹配所有数字
查找数字字符的快捷方式是\d小写字母d。这等于字符类[0-9],它查找零到九之间任意数字的单个字符。

let movieName = "2001: A Space Odyssey";
let numRegex = /\d/g; // Change this line
let result = movieName.match(numRegex).length;

18、匹配所有非数字
查找非数字字符的快捷方式是\D. 这等于 character class [^0-9],它查找不是介于 0 和 9 之间的数字的单个字符。

let movieName = "2001: A Space Odyssey";
let noNumRegex = /\D/g; // Change this line
let result = movieName.match(noNumRegex).length;

19、匹配空格
\s可以使用小写的搜索空格s。此模式不仅匹配空格,还匹配回车符、制表符、换页符和换行符。类似于字符类[ \r\t\f\n\v]。

let sample = "Whitespace is important in separating words";
let countWhiteSpace = /\s/g; // Change this line
let result = sample.match(countWhiteSpace);

20、匹配非空白字符
\S使用大写的 搜索非空格s。此模式将不匹配空格、回车、制表符、换页符和换行符类似于 character class [^ \r\t\f\n\v]。

let sample = "Whitespace is important in separating words";
let countNonWhiteSpace = /\S/g; // Change this line
let result = sample.match(countNonWhiteSpace);

21、指定匹配的上限和下限
可以使用数量说明符指定模式的下限和上限数量。数量说明符与大括号 ({和}) 一起使用。您在大括号之间放置两个数字 - 用于模式的下限和上限。

let ohStr = "Ohhh no";
let ohRegex = /Oh{3,6}\sno/; // Change this line
let result = ohRegex.test(ohStr);

22、仅指定较低的匹配数
有时只想指定没有上限的模式数量的下限。要仅指定较低数量的模式,请保留第一个数字后跟逗号。

let haStr = "Hazzzzah";
let haRegex = /Haz{4,}ah/; // Change this line
let result = haRegex.test(haStr);

23、指定确切的匹配数
有时只需要特定数量的匹配项。要指定一定数量的模式,只需在大括号之间放置一个数字。

let timStr = "Timmmmber";
let timRegex = /Tim{4}ber/; // Change this line
let result = timRegex.test(timStr);

24、检查全部或无
可以使用问号来指定可能存在的元素,?。这将检查零个或前面的元素之一。

let favWord = "favorite";
let favRegex = /favou?rite/; // Change this line
let result = favRegex.test(favWord);

25、使用捕获组进行搜索和替换

let str = "one two three";
let fixRegex = /(\w+)\s(\w+)\s(\w+)/; // Change this line
let replaceText = "$3 $2 $1"; // Change this line
let result = str.replace(fixRegex, replaceText);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值