javascript - 如何在 nodejs 中用单个正则表达式匹配多个字符串?

标签 javascript node.js regex

我的问题

我对以下代码片段感到困惑。这些字符串在正则表达式方面似乎相同(唯一的区别是应该与 \d 匹配的数字。本质上,第一个字符串匹配而第二个字符串不匹配。

在尝试之后,很明显顺序很重要:只有第一个字符串被匹配。

const regex = /departure\stime\s+([\d:]+)[\s\S]*arrival\stime\s+([\d:]+)[\s\S]*Platform\s+(\S+)[\s\S]*Duration\s([\d:]+)/gm;
const s1 = '\n                                departure time 05:42\n                                \n                                arrival time 06:39\n                                Boarding the train from Platform 3\n                                \n                                    Switch train in \n                                    No changing\n                                    \n                                        Change\n                                        \n                                    \n                                \n                                \n                                    Access for handicapped.\n                                    reserved seats\n                                \n                                \n                                    Duration 00:57\n                                \n                            ';
const s2 = '\n                                departure time 05:12\n                                \n                                arrival time 06:09\n                                Boarding the train from Platform 3\n                                \n                                    Switch train in \n                                    No changing\n                                    \n                                        Change\n                                        \n                                    \n                                \n                                \n                                    Access for handicapped.\n                                    reserved seats\n                                \n                                \n                                    Duration 00:57\n                                \n                            ';
console.log('Match:   ', regex.exec(s1));
console.log('No Match:', regex.exec(s2));

我的问题

如何使用相同的正则表达式来匹配多个字符串,而不用担心之前的匹配可能会改变匹配?

最佳答案

当您在正则表达式中使用“g”标志时,.exec() 将返回正则表达式对象上 lastIndex 属性的索引。然后,当您尝试对同一正则表达式再次使用 .exec() 时,它将在 lastIndex 中指定的索引处开始搜索。

有几种方法可以克服这个问题:

1) Remove the 'g' flag. lastIndex will stay set at 0
2) Use .match(), .test(), or .search()
3) Manually reset the lastIndext after each call to .exec()
    // for example:
    let results = regex.exec(s1);
    regex.lastIndex = 0;

在此处查看文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec

关于javascript - 如何在 nodejs 中用单个正则表达式匹配多个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47756343/

相关文章:

JavaScript jQuery 每秒触发一次点击

javascript - wrap() 与 after() 不能很好地配合

javascript - 创建添加 CORS header 的 React App

c# - 改变字符串的中间位置

c - 执行 C 文件时 sed 命令不起作用

java - 在java中从csv文件中提取字符串

javascript - 通过 Google Sheets Apps 脚本访问 Telegram Bot API 文件

javascript - 当我们使用 Jquery/Javascript 单击下一个和上一个按钮时动态添加图像

node.js - MongoDb中基于日期的数据清除逻辑

node.js - 使用 webpack 创建 npm 包