javascript - 为什么这两个 JavaScript 正则表达式会产生不同的结果?
问题描述
首先,我有:
const regEx = /(?:\S+\s)?\S*text\S*(?:\s\S+)?/;
const fullText = "one two text three four";
console.log(fullText.match(regEx));
这导致
['二文字三',索引:4,输入:'一二文字三四']
然后我有:
const regEx2 = new RegExp("(?:\S+\s)?\S*text\S*(?:\s\S+)?", "g");
const fullText2 = "one two text three four";
console.log(fullText2.match(regEx2));
这导致:
[ '文本' ]
使用new RegExp
,如何获得与第一个代码块相同的结果?
解决方案
推荐阅读
- c - 从文件读取时忽略前两个单词
- php - 使用 .htaccess 和 Mod 重写通过 API 强制请求图像文件
- typescript - 打字稿可能的代码路径与打字漏洞?
- javascript - Node.js 在 require 上调用函数
- java - Spring boot - 在多租户应用程序中的单个请求中访问多个数据源
- python - Python:不使用任何功能的排序列表中最常见的元素(模式)
- python - How are escape characters counted towards the length of a string?
- ios - Chart path animation
- javascript - Codeigniter 分页和 List.js
- python - is there a more efficient way to aggregate a dataset and calculate frequency in Python or R?