[英]Match everything in string except in quotes
I am trying to match anything except between ' quotes '. 我正在尝试匹配除“引号”之间的所有内容。 If possible between '
"
`
quotes (I know I can use ['"`]
). 如果可能,请在'
"
`
引号之间(我知道我可以使用['"`]
)。
Here is my regex pattern which just gets all text in between '
"
`
. 这是我的正则表达式模式,它只获取'
"
`
之间'
所有文本。
^((?!\'.*\').*)$
Note: I am talking about JavaScript Regex, therefor, I don't need PHP or Python regex patterns. 注意:我说的是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')
Match the " ' ** and then use that match to find the other one, after matching any characters besides the one of the **" '
that we just found 匹配“' ** and then use that match to find the other one, after matching any characters besides the one of the **" '
我们刚刚发现** 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, '')
If you want it to match no characters in between marks, as well, then: 如果您也希望它在两个标记之间不匹配任何字符,则:
string.replace(/(['"`]).*?\1/g, '')
To separate the quotes by type so they do not get mixed (ex. "words'
), \\B
: Non-word Boundary meta sequence was wrapped around each QUOTE .+?
QUOTE and then each of those expressions separated by |
: OR. 为了按类型分隔引号,以使引号不会混淆(例如"words'
), \\B
:非单词边界元序列被包裹在每个QUOTE .+?
QUOTE周围,然后每个表达式用|
:OR分隔。
/(\B(".+?")\B|\B('.+?')\B|\B(`.+?`)\B)/g;
Alternatively, QUOTE .*?
或者, QUOTE .*?
QUOTE could be used if you expect empty quotes as well. 如果您也希望使用空引号,则可以使用QUOTE 。
var rgx = /(\\B(".+?")\\B|\\B('.+?')\\B|\\B(`.+?`)\\B)/g; var str = `"Lorem ipsum dolor sit amet", consectetur adipisicing elit, "'sed do eiusmod tempor,'" incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, \\`quis nostrud\\` exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. \\`Duis aute irure dolor\\` in "reprehenderit in voluptate" velit esse cillum 'dolore eu fugiat nulla' pariatur. `; var sub = ``; var res = str.replace(rgx, sub); console.log(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.