繁体   English   中英

带引号和双引号的条件正则表达式

[英]Conditional regex on quotes and double quotes

我有这个正则表达式:

/templateUrl:\s*'([^']+?\.html)'/g

我也试图接受双引号。 到目前为止,我了解到这是创造条件的方式:

(?(?=regex)then|else)

所以我会得到:

/templateUrl:\s*(?(?=')([^']+?\.html')|(?(?=")([^"]+?\.html")))/g

但这不起作用..我还缺少什么?

编辑:

巨型OR呢?

/templateUrl:\s*('([^']+?\.html)')|("([^"]+?\.html)")/g

正如Pointy所说的,最终的解决方案具有更好的性能

/(templateUrl:\s*)(['"])([^\2]+?\.html\2)/g

[abdd]比a | b | c | d快很多

最终解决方案与反向引用

(templateUrl:\s*)('|")([^\2]+?\.html\2)

使用OR的先前解决方案(也可以使用)

在这里测试

(templateUrl:\s*)('([^']+?\.html)'|"([^"]+?\.html)")

在这里测试

您也有这种运作良好的可能性

(templateUrl:)(?:\\s*)(["|'](.*\\.html)["|'])

编辑以吸收反引号

templateUrl:\\s*([<="|'|`].*\\.html[="|'|`])

您将捕获没有多余空间的templateUrl不带引号test.html的html路径和带引号"test.html"'test.html'或`test.html`的路径。

希望对您有所帮助:)

暂无
暂无

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

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