简体   繁体   English

检查JavaScript中的字符串中的数字

[英]Checking the number inside a string in javascript

I have a string, say 我有绳子说

var Str = 'My name is 123 and my name is 234' . var Str = 'My name is 123 and my name is 234'

Now I split this as 现在我将其拆分为

var arrStr = Str.split(' ');

I iterate through the array and have different logic depending upon whether the word is a string or number. 我遍历数组,并根据单词是字符串还是数字来使用不同的逻辑。 How do i check that? 我该如何检查? I tried typeof which didn't work for me. 我尝试了对我不起作用的typeof

EDIT: 编辑:

After Seeing multiple answers. 看到多个答案之后。 Now, I am in despair, which is the most efficient way? 现在,我感到绝望,这是最有效的方法?

If you care only about the numbers, then instead of using split you can use a regular expression like this: 如果只关心数字,则可以使用如下正则表达式来代替使用split:

var input = "My name is 123 and my name is 234";
var results = input.match(/\d+/g)

If you care about all pieces, then you can use another expression to find all non-space characters like this: 如果您关心所有部分,则可以使用另一个表达式查找所有非空格字符,如下所示:

var input = "My name is 123 and my name is 234";
var results = input.match(/\S+/g)

Then iterate them one by one, and check if a given string is a number or not using the famous isNumeric() function posted by @CMS in this famous question . 然后一个接一个地迭代它们,并使用@CMS在此著名问题中发布的著名 isNumeric isNumeric()函数检查给定的字符串是否为isNumeric()

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

NOTE: Thanks to @Pointy and if you want them as numbers, input.match(/\\d+/g).map(Number) . 注意:感谢@Pointy,如果希望将它们用作数字,请input.match(/\\d+/g).map(Number)

You need to attempt to convert your array values to an integer. 您需要尝试将数组值转换为整数。

To iterate them you can use a for loop: 要迭代它们,可以使用for循环:

for(i=0;i<arrStr.length;i++) {
    var result = !isNaN(+arrStr[i]) ? 'number' : 'string';
    console.log(result);
}

Here I'm using a unary + to attempt to convert the value of each array value to a number. 在这里,我使用一元+尝试将每个数组值转换为数字。 If this fails, the result will be NaN . 如果失败,则结果为NaN I'm then using JavaScript's isNaN() method to test if this value is NaN . 然后,我使用JavaScript的isNaN()方法测试此值是否为NaN If it isn't, then it's a number, otherwise it's a string. 如果不是,则为数字,否则为字符串。

The result of this using the string you've provided is: 使用您提供的字符串的结果是:

string
string
string
number
string
string
string
string
number

To use this in an if statement, we can simply: 要在if语句中使用它,我们可以简单地:

for(i=0;i<arrStr.length;i++) {
    if(isNaN(+arrStr[i])) {
        /* Process as a string... */
    }
    else {
        /* Process as a number... */
    }
}

JSFiddle demo . JSFiddle演示

To expound on Sniffer's answer... 详细解释Sniffer的答案...

var input = "My name is 123 and my name is 234";
var numberArray = input.match(/\d+/g);
var wordArray = input.match(/[A-Za-z]+/g);

for (var number in numberArray)
{
    //do something
}
for (var word in wordArray)
{
    //do something
}

While researching, I found out about the Number() object. 在研究时,我发现了Number()对象。 This is generally used to work with manipulation of numbers. 通常用于处理数字。 MDN has a good documentation . MDN有一个很好的文档。

I found out that Number() returns NaN (Not a Number) when not passed a number. 我发现Number()未传递数字时返回NaN (不是数字)。 Since no number returns NaN, It could be a good way to check whether the passed object is string or a number literal. 由于没有数字返回NaN,因此这可能是一种检查传递的对象是字符串还是数字文字的好方法。

So my code would be: 所以我的代码是:

if (Number(arrStr[i]) == NaN){
    //string
} else  {
    //number
}

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

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