简体   繁体   中英

Exclude beginning string from regex

I have a piece of regex that looks like this:

/^.*website.localdev$/

This matches website.localdev as well as any subdomain like www.website.localdev fine.

I need to adapt it to exclude a string ("foo") at the beginning of the regex, so that "website.localdev" and "www.website.localdev" still matches but "foo.website.localdev" does not.

You should use negative lookahead :

/^(?!foo\.).*?website.localdev$/

You can see it in action on rubular

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