[英]Javascript regex expression text between """
如何在 javascript 的正则表达式中获取"""
和"""
之间的文本?
例子:
"""hello world"""
应该给hello world
""" hi """ """world"""
应该给[hi, world]
干得好:
(?<=""") ?\w+ ?( \w+)*(?=""")
解释
(?<=""") ---> make sure three quotes appear prior to a word
? ---> optional space
\w+ ---> match any word characters
( \w+)* ---> match 0 or more spaced out words
(?+""") ---> ensure that there's following close quotes
在这里看到它的实际效果
不确定 OP 是否仍在寻找答案,但您应该尝试:
/"""([^"""]+)"""/g
这将匹配""" hi """ """world""" """hello world"""
甚至"""I've bought a phone that costs $1000.00"""
以下 JavaScript 正则表达式应提取三重双引号之间的所有字符串:
var regexp = /"{3}([^"]*?)"{3}/g;
const regex = /"{3}([^"]*?)"{3}/g; const str = `"""hello world""" """ hi """ """world""" """ hi """ """world""" hello world How are you? `; 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}`); }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.