简体   繁体   English

Javascript:如何检查字符串类型?

[英]Javascript: How to check string type?

So I would like to know what's inside the string for example: 所以我想知道例如在字符串里面是什么:

var str = "a"; // Letter
var str = "1"; // Number
var str = "["; // Special
var str = "@"; // Special
var str = "+"; // Special

Is there any pre defined javascript function for this? 是否有为此预先定义的javascript函数? Otherwise I will make it with regex :) 否则我会用正则表达式来实现它:)

if (/^[a-zA-Z]$/.test(str)){
    // letter
} else if (/^[0-9]$/.test(str)){
    // number
} else {
    // other
};

Of course this only matches one character so 'AA' would end up in the //other section. 当然,这仅匹配一个字符,因此“ AA”将在//other部分结束。

They are all strings... 他们都是弦乐...

There isn't anything built in that will do what you want. 内置没有任何功能可以满足您的需求。

A regex may be a good solution, though you have not really provided enough information for one. 正则表达式可能是一个很好的解决方案,尽管您实际上并未提供足够的信息。

if(isNaN(string)){
   //yes is a string
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM