Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

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);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #filter #characters #words #javascript #array
ADD COMMENT
Topic
Name
9+4 =