This example is displayed only sam. How to make him find both sam , and so samwise?
var regex = /sam|samwise|merry|pippin/g;
var string = 'samwise gamgee';
var match = string.match(regex);
console.log(match);
Note. This is a simple example, but my real regular expressions are created by connecting 500 keywords in time, so it’s too cumbersome to look for all overlapping ones and make a special case for them with something like /sam(wise)/. Another obvious solution that I can think of is to simply repeat all the keywords individually, but I think it should be a quick and elegant solution with one regular expression.
source
share