简体   繁体   English

Sublime Text:复杂的正则表达式-搜索和替换

[英]Sublime Text: complicated regex - search and replace

I wanted to do a quick test and see if <span ng-bind="test.id"></span> would be better than using {{test.id}} for data-binding. 我想做一个快速测试,看看<span ng-bind="test.id"></span>是否比使用{{test.id}}进行数据绑定更好。 Would there by a way I could do this in regex for the whole app? 我是否可以在整个应用的正则表达式中做到这一点?

I'd have to make sure that it doesn't replace variables that are inside of a tag like <div id="chat-{{otherPerson.id}}" ...> , so it would only be replacing things like <div class="message">{{message.body}}</div> . 我必须确保它不会替换<div id="chat-{{otherPerson.id}}" ...>之类的标记内的变量,因此它只能替换<div class="message">{{message.body}}</div>

I'd also need to be able to check if there was a filter being used: {{ otherPerson | fullname}} 我还需要能够检查是否使用了过滤器: {{ otherPerson | fullname}} {{ otherPerson | fullname}}

Is this even possible with regex, or would I need to write a grunt task to take care of it? 使用regex甚至可以实现,还是我需要编写一个艰巨的任务来解决呢?

I believe you should be able to use this type of regex 我相信您应该可以使用这种正则表达式

/([^'"])\{\{([^\}|]+)\}\}/gi
  1. match 1 = anything that doesn't start with a single or double quote 匹配1 =任何不以单引号或双引号开头的内容
  2. then has two open curly braces 然后有两个开口的花括号
  3. match 2 = anything (if it's got one or more chars) inside those braces up till that's not a pipe or close curly brace 匹配2 =大括号内的所有内容(如果有一个或多个字符),直到不是竖线或大括号为止
  4. then has two closing curlies 然后有两个闭合的冰壶
  5. global and case-insensitive searching flags 全局且不区分大小写的搜索标志

and replace with 并替换为

'$1<span ng-bind="$2"></span>'

Hope it helps! 希望能帮助到你!

I think you can use this regex: 我认为您可以使用此正则表达式:

/(^|(>[^\<]*)|(^[^\<]*))(\{\{[^\{\}]*\}\})/igm

and use its substitution $4 . 并使用其替换$4

If I got you correctly, you want to replace 如果我没错,您想更换

>\s*\{\{([^|}<]+)\}\}

with

 ng-bind="\1">

Note that the replacement has space in front. 请注意,替换项在前面有空间。

For the filter case you can replace 对于过滤器箱,您可以更换

>\s*\{\{([^|}<]+\|[^|}<]+)\}\}

with

 ng-bind="(\1)">

You could try multiple pass approach 您可以尝试多次通过方法

  1. Search regex "(.*)\\{\\{([^\\|]+?)\\}\\}(.*)" replace with "$1[[[$2]]]$3" 搜索正则表达式"(.*)\\{\\{([^\\|]+?)\\}\\}(.*)"替换为"$1[[[$2]]]$3"
  2. Search regex \\{\\{\\s*([^\\|]+?)\\s*\\}\\} replace it with <span data-bind="$1"></span> 搜索正则表达式\\{\\{\\s*([^\\|]+?)\\s*\\}\\}替换为<span data-bind="$1"></span>
  3. Search regex \\[\\[\\[ replace with {{ 搜索正则表达式\\[\\[\\[{{
  4. Search regex \\]\\]\\] replace with }} 搜索正则表达式\\]\\]\\]替换为}}

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

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