Search
 
SCRIPT & CODE EXAMPLE
 

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
Comment

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
Comment

PREVIOUS NEXT
Code Example
Javascript :: Replace empty strings in object with null values 
Javascript :: how to clear all slash commands 
Javascript :: jvectormap color regions 
Javascript :: django csrf failed ajax 
Javascript :: div diseapear animation 
Javascript :: javascript tousand seperator 
Javascript :: javascript max characters string function 
Javascript :: javascript const 
Javascript :: number vs bigint js 
Javascript :: promise catch javascript 
Javascript :: three ways of writing a function in javascript 
Javascript :: EXPRESS APP REGISTER HANDLEBARS VIEW ENGINE 
Javascript :: VS Code Auto Import is bugging usestate 
Javascript :: clear input fild 
Javascript :: datatable buttons bootstrap 4 
Javascript :: How create a function that return element in js 
Javascript :: javascript ean13 checksum generate 
Javascript :: number of edges between set of nodes networkx 
Javascript :: json regex 
Javascript :: react hooks in codepen 
Javascript :: Get Country from the international phone number 
Javascript :: what is ... in javascript 
Javascript :: window.focus and window.blur jquery 
Javascript :: Assume that x is a char variable has been declared and already given a value. Write an expression whose value is true if and only if x is a upper-case letter. 
Javascript :: vuejs pass data to router-view 
Javascript :: node js create or check directory 
Javascript :: get field type file js and loop 
Javascript :: react useeffect on change props 
Javascript :: javascript get user from api 
Javascript :: pass arguments into require javascript 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =