Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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_Arrays_String_Parsing - Fatal编程技术网

如何在Javascript中识别字符串中的第一个字符是否为数字

如何在Javascript中识别字符串中的第一个字符是否为数字,javascript,arrays,string,parsing,Javascript,Arrays,String,Parsing,当给定字符串时,例如 address = "12345 White House Lane" 有没有办法确定字符串的第一个字符(“上例中的1”)是数字还是字符?在我的情况下,我需要能够识别它是否是一个数字,如果是,则删除地址的数字部分(只留下街道名称) 我尝试过使用isNaN函数 if(isNaN(address[0]){ //Do this or that } 但有人告诉我,它不够可靠,无法广泛使用。还有人告诉我可以使用类似于 if(address.matches("[0-9].

当给定字符串时,例如

address = "12345 White House Lane" 
有没有办法确定字符串的第一个字符(“上例中的1”)是数字还是字符?在我的情况下,我需要能够识别它是否是一个数字,如果是,则删除地址的数字部分(只留下街道名称)

我尝试过使用isNaN函数

if(isNaN(address[0]){
    //Do this or that
} 
但有人告诉我,它不够可靠,无法广泛使用。还有人告诉我可以使用类似于

if(address.matches("[0-9].*")){
    //Do this or that
}

但这似乎只是抛出类型错误,我不完全理解

您可以使用正则表达式将其删除,该正则表达式将查找起始数字和可能的空白

var address=“12345白宫巷”;
地址=地址。替换(/^\d+\s*/,“”);

控制台日志(地址)您可以使用如下函数将其拆分为两个可识别的部分:

const splitAddress=地址=>{
让{1:number,2:street}=address.match(/^\s*(\d*)\s*(.+)/)
返回{街号}
}
console.log(splitAddress('12345 Main St'))/=>{“number”:“12345”,“street”:“Main St”}
console.log(splitAddress('Main St'))/=>{“number”:“,”street:“Main St”}

console.log(splitAddress('219 W 48 th St'))/=>{“number”:“219”,“street”:“W 48 th St”}
您可以使用这个
/^\d/.test(word)
word.match(/[0-9]./)
word
是如何从
地址
派生出来的?