I have a javascript function which should populate a select box with all items from an array starting with the letter passed to the function. The only problem I have is that I can’t get my regex statement/coding to work. Here is my function:
function replaceCompanySelect (letter)
{
var list = document.getElementById("company"); //Declare the select box as a variable
list.options.length=0; //Delete the existing options
list.options[0]=new Option("Please Select a Company", "0", false, false); //Add the first option in
for(var i=1;i<companies.length;i++) //For each company in the array
{
if(companies[i].match("/\b"+letter+"/g") != null && (letter != 'undefined' ||letter != 'undefined')) //If the company starts with the correct letter and the position's value is not undefined or empty
{
alert(companies[i]); //Only used for testing purposes, code should be as above loop to I used to insert the 1st option
}
}
}
Any ideas?
This works, and without RegEx, too: