简体   繁体   English

正则表达式:匹配除部分匹配之外的所有内容

[英]REGEX: Match everything except partial match

I want to match everything except a given string (partial match) : https://play.google.com/variable 我想匹配除给定字符串(部分匹配)之外的所有内容: 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 此处提供完整代码: 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: 如果可能的话,我也很喜欢jsfiddle,它将使用修改后的regex除去该URL以外的所有内容,这样最终输出将是:

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 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. 请注意,我们仍然可以成功匹配URL的子字符串,例如“ google”和“ variable”,同时仍排除完整的URL匹配。

Edit: 编辑:

Here's another way to do it without the lookahead, using The Greatest Regex Trick Ever from Rexegg.com: 这是使用Rexegg.com的The Greatest Regex Trick Ever进行的另一种无需先行的方法:

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

And return group 1: 并返回组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 ^([\\ s \\ S] )?(\\ S )(https://play.google.com/\\S*)([\\ s \\ S] *)\\ g

then replace: $1$2$4 然后替换:$ 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. PS仅在文本中只有1个短语“ play.google.com”的情况下才有效。

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

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