我正在尝试获得一个正则表达式,它将在下面的示例中找到text1
和text2
:
,"blabla "test1" blabla", "another text"
,"blabla "test2" blabla", "another text"
总而言之,我希望所有双引号之间的文本,以及双引号和逗号之间的文本。
答案 0 :(得分:-1)
此表达式可能会这样做:
".+?"(.+?)".+?"
,我们想要的输出位于该捕获组中:
(.+?)
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}`);
});
}