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

filter a characters from words in javascript array

var strings = ["Cairo", "Casablanca", "Cangzhou", "Carcas"]; // Your array of things to filter

function disp(matches) {
 
    // Displays the filtered results on the page
    var results = document.getElementById("suggestions");
    results.innerHTML = matches.join("<br>");

}

function regExpTest(inputString, regex) {
    
    // Checks to see if what the user typed matches any items in gucciArray
    return regex.test(inputString);

}

function typeKey(event) { // This function is supposed to be called whenever the user types a letter in the text box
      
    var textTyped = event.target.value; // What the user typed in the text box;

    var filter = new RegExp(textTyped.split('').join('.*'), 'i'); // Matches anything with the characters that the user typed, in order, with any characters inbetween them
  
    // Filter the array
    // Returns a new array containing the items from the old array that pass the RegExp test
    var filteredArray = strings.filter(s => regExpTest(s, filter));

    // Display the filtered results on the page
    disp(filteredArray);

}

disp(strings);
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 :: what is runtime in javascript 
Javascript :: concatenate strings jsonata 
Javascript :: how-to-disable-remote-js-debugging-in-react-native 
Javascript :: Fromdata 
Javascript :: disable a tag javascript void 
Javascript :: __v:0 in my data mongoose 
Javascript :: this.$moment.tz.guess() not working mozilla 
Javascript :: make a table of data from db in jsp 
Javascript :: how to show name of inactive also in react-navigation-material-bottom-tabs 
Javascript :: Checking if the first letter of the string is uppercase 
Javascript :: Scotch.io - Create a CRUD App with Node and MongoDB 2 App Foundation 
Javascript :: is element displayed js 
Javascript :: SHOPIFY CUSTOMER WITH REGISTRATION 
Javascript :: cloning an element 
Javascript :: utility javascript function list 
Javascript :: getComments 
Javascript :: date et heure javascript 
Javascript :: regular expression for beginners 
Javascript :: Backbone Render And Initialize 
Javascript :: Get JSON Values In Array Alternative Syntax 
Javascript :: send data to another page javascript 
Javascript :: react extends component App.defaultProps 
Javascript :: jquery check screen width 
Javascript :: validar fecha jquery 
Javascript :: disable submit button until form is fully validated 
Javascript :: prisma multiple queries in one query 
Javascript :: FirebaseError: Unknown field filter op. 
Javascript :: sort list by likes in javascript 
Javascript :: NodeJS Multi-Core Processors Example 
Javascript :: repeater javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =