简体   繁体   English

__('…')的正则表达式

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

I'm trying to get a regular expression to work and I'm having problems: I want to match everything that starts with 我正在尝试使用正则表达式,但遇到了问题:我想匹配所有以

__(' or __("  

and ends with 并以

') or ") 

I tried with 我尝试过

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

but they all have problems with this sample text: 但他们都对此示例​​文本有疑问:

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

who has the winning RegExp? 谁拥有获奖的RegExp?

Using the second example, use *? 在第二个示例中,使用*? for a non-greedy match and consider using a backreference for the quotes. 对于非贪婪的匹配,请考虑对引用使用反向引用。

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

Live Demo 现场演示

I guess that you want non-greedy match, then the best solution is to use: 我猜您想要非贪婪的匹配,那么最好的解决方案是使用:

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

Also, escaping the single and double quotes may help: 此外,转义单引号和双引号可能会有所帮助:

You can check it at this regex101 您可以在此regex101上进行检查

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM