簡體   English   中英

__('…')的正則表達式

[英]Regular expression for __(' … ')

我正在嘗試使用正則表達式,但遇到了問題:我想匹配所有以

__(' or __("  

並以

') or ") 

我嘗試過

/__\(['"][^']*['"]\)/g  and /__\(['"].*['"]\)/g

但他們都對此示例​​文本有疑問:

text that should not match
__('all text and html<a href="#">link</a> that should match') text that should not match __('all text and html<a     href="#">link</a>')
__("all text and html<a href="#">link</a>") text that should not match __("all text and html<a     href="#">link</a>")
other text that should not match

誰擁有獲獎的RegExp?

在第二個示例中,使用*? 對於非貪婪的匹配,請考慮對引用使用反向引用。

/__\((['"]).*?\1\)/g

現場演示

我猜您想要非貪婪的匹配,那么最好的解決方案是使用:

/__\(['"][^']*?['"]\)/g

此外,轉義單引號和雙引號可能會有所幫助:

您可以在此regex101上進行檢查

http://regex101.com/r/sM8yF9/1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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