简体   繁体   中英

REGEX: Match everything except partial match

I want to match everything except a given string (partial match) : https://play.google.com/variable

I managed to do exactly the opposite at the moment by using the following regex:

(\S*play\.google\.com\S*)

which does select the only string I actually want to keep, so I want my regex to select all the text EXCEPT the one above (basically the opposite of what I just did).

I tried negative lookahead but it didn't work, hope you can help me. Full code available here: https://regexr.com/3h4se

If possible, I'd also love a jsfiddle which will strip away everything except that url, using your modified regex, so that the final output will just be:

https://play.google.com/variable

Good luck!

How about like this, using a negative lookahead:

^(?!https:\\/\\/play\\.google\\.com\\/variable).*

Live example:

https://regexr.com/3h4sq

Note that we can still successfully match substrings of the URL like "google" and "variable" while still excluding the full URL match.

Edit:

Here's another way to do it without the lookahead, using The Greatest Regex Trick Ever from Rexegg.com:

https:\/\/play\.google\.com\/variable|(.*)

And return group 1:

$1

I'm not totally clear what you're trying to do; but this is what I came up with:

^([\\s\\S] )?(\\S )(https://play.google.com/\\S*)([\\s\\S]*)\\g

then replace: $1$2$4

I would capture everything before and after into groups. Like this:

/([^]*)play\.google\.com([^]*)/

That way all you need is to replace the original text with the group before and after. eg:

original_text.replace(/([^]*)play\.google\.com([^]*)/, '$1$2');

This will give you all text before joined with all text after.

PS This will only work if there is 1 and only 1 phrase "play.google.com" in the text.

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