regex - 用于捕获两个双引号中的文本的正则表达式
问题描述
我正在尝试获得一个正则表达式,它将找到text1
并text2
进入以下示例:
,"blabla "test1" blabla", "another text"
,"blabla "test2" blabla", "another text"
总而言之,我想要双引号之间的所有文本,以及双引号和逗号之间的文本。
解决方案
这个表达式可能会这样做:
".+?"(.+?)".+?"
我们想要的输出在这个捕获组中:
(.+?)
演示
const regex = /".+?"(.+?)".+?"/gm;
const str = `,"blabla "test1" blabla", "another text"
,"blabla "test2" blabla", "another text"`;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
推荐阅读
- reactjs - React Router 不更新 url 更改的内容
- vhdl - 当我将数据从慢时钟域交叉到快时钟域时,拉伸的数据不会成为处理问题吗?
- php - 已超过“max_connections_per_hour”
- corda - 在哪里可以找到 Corda 快速入门演示?
- c# - 将元素插入表时 Azure 通知应用程序
- .net-core - .NET Core 或 Standard 是否有 ProtectedConfigurationProvider?
- javascript - 如何使用数据集值更新输入字段?
- python - 基于计数列对熊猫数据框进行下采样
- java - android studio中带有setImageDrawable方法的NullPointerException
- spring-data - 查询以忽略字段 azure cosmos db (NoSQL) 上的大小写