// 示例代码:使用 match 函数查找字符串中的所有匹配项
const str = "The rain in SPAIN stays mainly in the plain.";
const regex = /ain/g; // 定义一个正则表达式,匹配 "ain",全局搜索
const matches = str.match(regex); // 使用 match 函数进行匹配
console.log(matches); // 输出: [ 'ain', 'ain', 'ain', 'ain' ]
// 解释说明:
// 1. `str.match(regex)`:在字符串 `str` 中使用正则表达式 `regex` 进行匹配。
// 2. `regex` 是一个正则表达式对象,`/ain/g` 表示查找所有出现的 "ain",并且 `g` 标志表示全局搜索。
// 3. 如果没有找到匹配项,`match` 函数将返回 `null`;否则返回一个包含所有匹配项的数组。
上一篇:js split()函数
下一篇:js pop()函数
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站