使用 JavaScript Regex,我需要基于以下字符串:
word1 w'or""d2 word3
"word4"word5
word6
有这样的结果:
word1
word2
word3
word4word5
word6
我试过这个:
\S+/gm
https://regex101.com/r/nsIA2Y/2
根据空格对单词进行正确分组,但是,我找不到删除 "和 ' 字符的方法,如示例中那样将分组的单词保持在一起。尝试了几种分组方法,但我的知识有限。
请同时说明解决方法。
最佳答案
这个问题可以简单地用字符串替换来解决,正则表达式似乎不是必需的,或者如果是这样,也许这个表达式可以简单地做到这一点。
const regex = /["']+/gm;
const str = `word1 f'dfretretr""ret word2 word3
"word4"word5
word6 word7
`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log(result);
表达式在 this demo 的右上面板中进行了解释, 如果你想探索/简化/修改它,在this link ,如果愿意,您可以逐步观察它如何与一些示例输入匹配。
关于javascript - 如何过滤保留原始组结果的先前正则表达式组的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56999607/