Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to filter an array of strings to see which letters match javascript

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
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #filter #array #strings #letters #match #javascript
ADD COMMENT
Topic
Name
1+1 =