[英]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
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 您可以尝试多次通过方法
"(.*)\\{\\{([^\\|]+?)\\}\\}(.*)"
replace with "$1[[[$2]]]$3"
"(.*)\\{\\{([^\\|]+?)\\}\\}(.*)"
替换为"$1[[[$2]]]$3"
\\{\\{\\s*([^\\|]+?)\\s*\\}\\}
replace it with <span data-bind="$1"></span>
\\{\\{\\s*([^\\|]+?)\\s*\\}\\}
替换为<span data-bind="$1"></span>
\\[\\[\\[
replace with {{
\\[\\[\\[
用{{
\\]\\]\\]
replace with }}
\\]\\]\\]
替换为}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.