可以通过 npm 或者直接引用脚本文件的方式来安装 XRegExp:
npm install xregexp
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/4.4.1/xregexp-all.min.js"></script>
const XRegExp = require('xregexp');
// 创建增强的正则表达式
const regex = XRegExp('(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})');
const date = '2024-06-26';
// 使用命名捕获组提取日期部分
const match = XRegExp.exec(date, regex);
console.log(match.year); // 输出 "2024"
console.log(match.month); // 输出 "06"
console.log(match.day); // 输出 "26"
const XRegExp = require('xregexp');
// 使用 XRegExp.replace 进行字符串替换
const result = XRegExp.replace('Hello, world!', 'world', 'XRegExp');
console.log(result); // 输出 "Hello, XRegExp!"
// 使用 XRegExp.test 进行模式匹配测试
const isMatch = XRegExp.test('abc123', '\\d+');
console.log(isMatch); // 输出 true
const XRegExp = require('xregexp');
// 创建包含 Unicode 支持的正则表达式
const unicodeRegex = XRegExp('\\p{L}+');
const text = 'こんにちは';
const unicodeMatch = XRegExp.exec(text, unicodeRegex);
console.log(unicodeMatch[0]); // 输出 "こんにちは"