簡體   English   中英

匹配字符串中除引號外的所有內容

[英]Match everything in string except in quotes

我正在嘗試匹配除“引號”之間的所有內容。 如果可能,請在' " `引號之間(我知道我可以使用['"`] )。

這是我的正則表達式模式,它只獲取' " `之間'所有文本。

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

Regex101鏈接

注意:我說的是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') 

匹配“' ** 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, '')

如果您也希望它在兩個標記之間不匹配任何字符,則:

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

為了按類型分隔引號,以使引號不會混淆(例如"words' ), \\B :非單詞邊界元序列被包裹在每個QUOTE .+? QUOTE周圍,​​然后每個表達式用| :OR分隔。

/(\B(".+?")\B|\B('.+?')\B|\B(`.+?`)\B)/g;

或者, QUOTE .*? 如果您也希望使用空引號,則可以使用QUOTE


RegEx101

演示版

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM