Closed miloshavlicek closed 4 years ago
How about:
const chars = 'aeiou+'; // or: ['a','e','i','o','u','+'].join('');
const re = XRegExp('[' + XRegExp.escape(chars) + ']+');
const result = XRegExp.match('heeeja+aola', re, 'all'); // ['eee','a+ao','a']
If it wasn’t for the bug described in #192 (where interpolation isn’t currently allowed inside character classes), you could replace the second line with const re = XRegExp.tag()`[${chars}]+`;
.
Note that for usage with plain JS regular expressions (i.e. without XRegExp), regenerate solves this problem. Given a set of characters, it produces a valid & ASCII-safe JS regular expression pattern that matches only those characters.
regenerate('a','e','i','o','u', '+').toString();
// --> '[\\+aeiou]'
Feature request (for discussion): It would be useful to be able to create a new character set from a string array of allowed chars.
At this time I have my own static function that escapes and joins chars from string array, however, I think it can be useful also for others.
Example implementation: