匹配所有字符串,除了引号

时间:2018-07-06 18:32:17

标签: javascript regex

我正在尝试匹配除“引号”之间的所有内容。在' " `引号之间(如果可以,我可以使用['"`])。

这是我的正则表达式模式,它仅获取' " `之间的所有文本。

^((?!\'.*\').*)$

Regex101 link

注意:我在说的是JavaScript正则表达式,因此,我不需要PHP或Python正则表达式模式。

let string = 'lorem \'ipsum\' dolor'
let match = string.match(/^((?!\'.*\').*)$/)
console.log('[===Real output===]')
console.log(match[1])
console.log('[===Expected output===]')
console.log('lorem  dolor')

2 个答案:

答案 0 :(得分:0)

匹配我们刚刚找到的“'** and then use that match to find the other one, after matching any characters besides the one of the **" '

string.replace(/(['"`])(?:(?!\1)[\s\S])+\1/g, '')

如果您也希望它在两个标记之间不匹配任何字符,则:

string.replace(/(['"`]).*?\1/g, '')

答案 1 :(得分:0)

要按类型分隔引号,以使引号不会混淆(例如"words'),\B:非单词边界元序列被包裹在每个 QUOTE { {1}} QUOTE ,然后用.+?分隔的每个表达式:或。

|

或者,如果您也希望使用空引号,则可以使用 QUOTE /(\B(".+?")\B|\B('.+?')\B|\B(`.+?`)\B)/g; QUOTE


RegEx101

演示

.*?