javascript - 在正则表达式之前捕获字符
问题描述
我有一个关于我用 JavaScript 编写的正则表达式的快速问题。它是以下内容(?<=,)(.*)(?=:)
,它捕获了 和 之间,
的所有内容:
。但是,我希望它也能捕获逗号本身,例如。
So,<< this is what my regex captures at the moment>>: end
会成为
So<<, this is what my regex captures at the moment>>: end
.
我尝试在正则表达式中使用 a.
之前,,
但它似乎不起作用。
解决方案
如你所说 :
所以,<<这就是我的正则表达式此刻捕获的内容>>:end 会变成所以<<,这就是我的正则表达式此刻捕获的内容>>:end。
你可以这样使用replace
:
var str = `So,<< this is what my regex captures at the moment>>: end`;
var replace = str.replace(/(.*?)(,)(<<)(.*)/,"$1$3$2$4");
console.log(replace);
推荐阅读
- android - CCPA 检测不跟踪,Google 帐户网络和应用程序活动关闭
- python - 如何根据 BeautifulSoup 中的显式匹配选择元素?
- ios - 如何保持 tableview 部分处于状态?
- mysql - 如何将 2 个 MySQL 查询组合或连接在一起
- java - 1 MB 或更大的 Java 字节数组占用两倍的 RAM
- jquery - 数据表,initComplete - 选择页眉,而不是页脚
- php - Wordpress 输出摘录作为链接无法正常工作?
- c# - 如何拆箱元组?
- flutter - 颤动贝塞尔曲线自动决定控制点
- ruby-on-rails - Webpacker 抛出 application.css not found in manifest.json in *Rails 6* application