Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript正则表达式所有名称都以_Javascript_Regex_Drop Down Menu - Fatal编程技术网

Javascript正则表达式所有名称都以

Javascript正则表达式所有名称都以,javascript,regex,drop-down-menu,Javascript,Regex,Drop Down Menu,我有一个javascript函数,它应该用从传递给函数的字母开始的数组中的所有项填充一个选择框。我唯一的问题是我不能让我的正则表达式/编码工作。以下是我的功能: function replaceCompanySelect (letter) { var list = document.getElementById("company"); //Declare the select box as a variable list.options.length=0; //Delete the exi

我有一个javascript函数,它应该用从传递给函数的字母开始的数组中的所有项填充一个选择框。我唯一的问题是我不能让我的正则表达式/编码工作。以下是我的功能:

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

    }

}

}
函数替换公司选择(字母)
{
var list=document.getElementById(“公司”);//将选择框声明为变量
list.options.length=0;//删除现有选项
list.options[0]=新选项(“请选择一家公司”,“0”,false,false);//在中添加第一个选项

对于(var i=1;i来说,在没有正则表达式的情况下,这实际上可能更有效(当然,这可以算作微优化…)

if (letter && companies[i][0].toLowerCase() === letter.toLowerCase()) { ... }

这是可行的,而且没有正则表达式:

if (companies[i].charAt(0).toLowerCase() == letter.toLowerCase()) {...}

我不会为正则表达式而烦恼。你只会制造问题。像这样的东西会管用的

var companies  = ["microsoft","apple","google"],
    startsWith = function(arr,match){

        var length = arr.length;

        for(var i=0; i < length; i+=1){

            if(arr[i].toUpperCase().lastIndexOf(match.toUpperCase(), 0) === 0){

                return arr[i];                           
            }

        }
    };

console.log(startsWith(companies,"g")); //=> returns google
var companys=[“微软”、“苹果”、“谷歌”],
startsWith=功能(arr,匹配){
变量长度=arr.length;
对于(变量i=0;i返回谷歌
类似于

function foo (letter) {
 var companies  = ["microsoft","apple","google"];
  return companies.filter(function(s) { return s.match(new RegExp(letter,"ig")); });
}

alert(foo("G")); //google

不是答案,而是一个问题:为什么字母应该有“未定义”的值?因为数组中的数字直接对应于数据库中的id号,所以会有一些数字没有值。您是舒尔吗?您的意思不是
typeof letter==“未定义”
?为什么要将其删除两次?
(字母!='undefined'| |字母!='undefined')
我不是故意的,这是一个复制粘贴错误,忘记更改第二个:/ty:)我想你可能想要
公司[I]。charAt(0)
,不?
。charAt(0)
可以工作,但也可以使用数组访问器语法访问字符(恰好更短).jmar777
string[index]
不是标准的,但是
string.charAt(index)
也是。也可能是有帮助的。数组访问器语法实际上是标准的(直到ES5才出现)。几乎所有JavaScript引擎都将字符串实现为“类似数组的”对象,所以数组访问器语法在ES5之前的实现中也是安全的。但有一点值得注意……如果您针对的是较旧版本的规范,它并不是严格的“标准兼容”。