const values = ['Brussels', 'Cairo', 'Casablanca', 'Cangzhou', 'Caracas',
'Los Angeles', 'Osaka'];
const match = (s) => {
const p = Array.from(s).reduce((a, v, i) => `${a}[^${s.substr(i)}]*?${v}`, '');
const re = RegExp(p);
return values.filter(v => v.match(re));
};
console.log(match('Ca')); // Cairo, Casablanca, Cangzhou, Caracas
console.log(match('Caz')); // Cangzhou
console.log(match('as')); // Casablanca, Caracas
console.log(match('aa')); // Casablanca, Caracas, Osaka
Run code snippet
const values = ['Brussels', 'Cairo', 'Casablanca', 'Cangzhou', 'Caracas',
'Los Angeles', 'Osaka'];
const match = (s) => {
const p = Array.from(s).reduce((a, v, i) => `${a}[^${s.substr(i)}]*?${v}`, '');
const re = RegExp(p);
return values.filter(v => v.match(re));
};
console.log(match('Ca')); // Cairo, Casablanca, Cangzhou, Caracas
console.log(match('Caz')); // Cangzhou
console.log(match('as')); // Casablanca, Caracas
console.log(match('aa')); // Casablanca, Caracas, Osaka
Run code snippet