简体   繁体   中英

Conditional regex on quotes and double quotes

I have this RegEx:

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

I'm trying to accept double quotes as well. What I learnt so far is that this is the way to make conditions:

(?(?=regex)then|else)

So I would get:

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

But this is not working..what am I missing?

EDIT:

what about a giant OR?

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

Atual final solution with better performance as Pointy was saying

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

[abdd] is a lot faster than a|b|c|d

Final solution with backreference :

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

Previous solution (also working) with OR

Test here

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

Test here

You have also this the possibility which work well

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

edited in order to take in count backticks

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

You will catch the templateUrl without the excess space the html path without quote test.html and the path with quote "test.html" or 'test.html' or `test.html`

I hope that can help you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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