简体   繁体   English

最小化崇高文本正则表达式搜索和替换中的子表达式

[英]Minimize sub-expression in sublime text regex search and replace

I am trying to replace 我正在尝试更换

<div id="logo"> <a href="index.html"><img src="images/Logo.png" width="205" height="50"  alt="logo"/></a><a class="menu-hider"></a></div>

with

<div id="logo"> <a href="index.html"><img src="{{ asset( 'images/Logo.png' }}" width="205" height="50"  alt="logo"/></a><a class="menu-hider"></a></div>

in Sublime text. 在Sublime文字中。 Based on the docs I figured this would work search 根据文档,我认为这可以进行搜索

src=\"(.*)\"

replace 更换

src=\"{{ asset('$1') }}\"

However instead of Sublime search catching 但是,不是捕捉Sublime搜索

src="images/Logo.png"

it catches the longest possible sub-expression and finds the last occurence of \\" 它捕获最长的子表达式并找到\\的最后一次出现

src="images/Logo.png" width="205" height="50"  alt="logo"/></a><a class="menu-hider"

this is instead of the first which is what I'm expecting. 这是我所期望的,而不是第一个。 What is wrong? 怎么了?

It's because .* is greedy, meaning it'll match as much as possible until it reaches \\" . Change it to the lazy form by adding ? : 这是因为.*是贪婪的,这意味着它将尽可能匹配,直到达到\\" 。通过添加?将其更改为惰性形式:

src=\"(.*?)\"
         ^

regex101 demo regex101演示

or use a negated class: 或使用否定的类:

src=\"([^\"]*)\"
       ^^^^^

regex101 demo regex101演示

The latter will match any character except " so there's no way it'll match a " . 后者将匹配"以外的任何字符,因此无法匹配"

Note: I don't remember the requirement to escape quotes in ST2. 注意:我不记得ST2中对转义引号的要求。 I can't test it right now, but it should be working fine without being escaped. 我现在无法对其进行测试,但是它应该可以正常运行而不会被逃脱。

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

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