ES6 正则的扩展(十九)

1. 正则表达式字面量改进

特性:在 ES6 中,正则表达式字面量允许在字符串中使用斜杠(/)作为分隔符。
用法:简化正则表达式的书写。

const regex1 = /foo/;
const regex2 = /foo/g; // 全局搜索

2. u 修饰符(Unicode)

特性:u 修饰符允许正则表达式正确处理 Unicode 字符。
用法:确保正则表达式在处理多字节字符时表现正确。

const regex = /foo/u;
console.log(regex.test("foö")); // 输出:false

3. y 修饰符(Sticky)

特性:y 修饰符使正则表达式在搜索时“粘”在每个匹配的开始位置。
用法:进行连续的匹配搜索。

const text = "abcabc";
const regex = /abc/y;

let match;
while ((match = regex.exec(text)) !== null) {
    console.log(`Found ${match[0]} at index ${match.index}`);
}
// 输出:
// Found abc at index 0
// Found abc at index 3

4. 新增的正则表达式方法

特性:String.prototype.match(), String.prototype.replace(), String.prototype.search(), 和 String.prototype.split() 现在可以接受正则表达式字面量。
用法:直接使用正则表达式进行字符串处理。

const text = "Hello World";
const regex = /Hello/;

console.log(text.match(regex)); // 输出:["Hello"]
console.log(text.replace(regex, "Hi")); // 输出:"Hi World"
console.log(text.search(regex)); // 输出:0
console.log(text.split(regex)); // 输出:["", " World"]

5. flags 属性

特性:正则表达式对象现在有一个 flags 属性,返回正则表达式的修饰符。
用法:获取正则表达式使用的修饰符。

const regex = /foo/g;
console.log(regex.flags); // 输出:"g"

6. dotAll 模式(点字符匹配所有)

特性:当设置了 s 修饰符(dotAll)时,点字符(.)可以匹配包括换行符在内的任何字符。
用法:进行多行匹配。

const text = "foo\nbar";
const regex = /bar/s; // 使用 dotAll 模式

console.log(regex.test(text)); // 输出:true

7. hasIndices 属性

特性:hasIndices 属性用于指示正则表达式是否捕获分组。
用法:检查正则表达式是否包含捕获组。

const regex1 = /foo/;
const regex2 = /foo(bar)/;

console.log(regex1.hasIndices); // 输出:false
console.log(regex2.hasIndices); // 输出:true

8. Symbol.match, Symbol.replace, Symbol.search, Symbol.split

特性:这些 Symbol 属性允许使用正则表达式进行字符串匹配、替换、搜索和分割。
用法:提供一种更灵活的方式来处理字符串。

const text = "Hello World";
const regex = /world/i;

console.log(text[Symbol.match](regex)); // 输出:["World"]
console.log(text[Symbol.replace](regex, "there")); // 输出:"Hello there"
console.log(text[Symbol.search](regex)); // 输出:4
console.log(text[Symbol.split](regex)); // 输出:["Hello ", ""]

相关推荐

  1. ES6 扩展

    2024-07-20 10:00:06       20 阅读
  2. ES6 对象扩展五)

    2024-07-20 10:00:06       22 阅读
  3. ES6 数值扩展八)

    2024-07-20 10:00:06       12 阅读
  4. ES6 数组扩展六)

    2024-07-20 10:00:06       16 阅读
  5. ES6扩展

    2024-07-20 10:00:06       35 阅读
  6. ES6—运算符扩展

    2024-07-20 10:00:06       28 阅读
  7. 【07】ES6:对象扩展

    2024-07-20 10:00:06       36 阅读
  8. 【08】ES6:运算符扩展

    2024-07-20 10:00:06       50 阅读
  9. ES6之函数新增扩展

    2024-07-20 10:00:06       52 阅读
  10. ES6之数组新增扩展

    2024-07-20 10:00:06       56 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-20 10:00:06       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-20 10:00:06       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-20 10:00:06       45 阅读
  4. Python语言-面向对象

    2024-07-20 10:00:06       55 阅读

热门阅读

  1. golang中实现LRU-K算法(附带单元测试)

    2024-07-20 10:00:06       20 阅读
  2. 23年阿里淘天笔试题 | 卡码网模拟

    2024-07-20 10:00:06       17 阅读
  3. 前端经验:使用sheetjs导出CSV文本为excel

    2024-07-20 10:00:06       16 阅读
  4. autohotkey自动化执行vim命令

    2024-07-20 10:00:06       20 阅读
  5. 开源虚拟加密盘VeraCrypt命令行使用方法

    2024-07-20 10:00:06       14 阅读
  6. DP 203 学习笔记

    2024-07-20 10:00:06       16 阅读
  7. python实现建立一个学生成绩管理系统

    2024-07-20 10:00:06       19 阅读
  8. redis是如何实现过期时间一到就删除key

    2024-07-20 10:00:06       20 阅读