Adobe CQ5 regex that will reject an entire string

2019-07-21 14:20发布

问题:

Adobe CQ5 dialog editor, regex property:

I am regex challenged so forgive me, but it seems a simple task and I just can't seem to find the right fit elsewhere on the internet. Every suggestion that looks right just doesn't seem to work in adobe's regex field.

I want the content admin to supply a relative URL to a "thank you" page. I don't need anything super fancy, I simply want to reject the admin's input if "http" or ".com" is found anywhere within the text supplied by the admin.

Examples for admin supplied input:
URL                                       REGEX VALIDATES INPUT AS SUCCESS?
http://www.domain.com/questions           No. string has both "http" and ".com"
https://www.domain.com/mail               No. string has both "http" and ".com"
https://www.domain.net                    No. string has "http"
oopshttp://www.domain.net                 No. string has "http"
www.domain.com/mail                       No. string has ".com"
/subdomain/thankyou.html                  YES! string has neither "http" or ".com"

Ideally, the rejection of "http" and ".com" would supplement what I currently have: Which is the string must start with a forward slash "/" and end in ".html" which is:

/^\/.+?\.html$/

Any help is appreciated, thank You in advance

回答1:

This can work with negative lookahead:

^\/(?!.*?(http|\.com)).+?\.html$

Online Demo: http://regex101.com/r/dN8uV2



回答2:

^/.+\.html$ should work completely fine.

Autopsy:

  • ^ the start MUST match whatever is next:
  • / a literal slash (if your modifier is a slash you need to escape this to \/)
  • .+ match as much as possible (any character 1 to infinity times)
  • \.html the literal string .html
  • $ the end MUST be here

Debuggex Demo