JS常用正则表达式备忘录
摘要: 玩转正则表达式。 原文:JS常用正则表达式备忘录 作者:前端小智 Fundebug经授权转载,版权归原作者所有。 正则表达式或“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); // ...

