My current regex looks for a searchQuery
inside sentences and matches them if those queries start a with a blank space, and end with either a blank space or ?!,.
. It generally works well, except for URLs. The regex ends up picking up urls and messing them up.
For example, if I was looking for "bitcoin" in a sentence "Bitcoin price is going nuts", it would find it, but it was also take the following url and match it.
https://versionone.vc/the-solar-
bitcoin-convergence
, messing up the url.
How can I tell JavaScript Regex to ignore any matches where the character before the matching words is either of these / - . _ +
? This will essentially eliminated matches inside urls?
Current Regex:
var reg = new RegExp('(\b)${searchQuery}(\s+|\.|\,|\?|\!', 'gi');
Replacement function:
newString = oldString.replace(reg, substringReplacement);
substringReplacement(match)
is a function that contains the logic of how to change the matching text.
Alternatively, what's another way to outright ignore urls from the searchable area.
Thanks!
question from:
https://stackoverflow.com/questions/65850978/how-to-exclude-regex-matches-from-inside-urls-with-javascript 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…