字符串的JQuery过滤器数量[重复]

时间:2022-11-29 11:09:54

This question already has an answer here:

这个问题在这里已有答案:

How do you filter only the numbers of a string?

你如何只过滤字符串的数字?

Example Pseudo Code:
number = $("thumb32").filternumbers()
number = 32

2 个解决方案

#1


67  

You don't need jQuery for this - just plain old JavaScript regex replacement

你不需要jQuery - 只需简单的旧JavaScript正则表达式替换

var number = yourstring.replace(/[^0-9]/g, '')

var number = yourstring.replace(/ [^ 0-9] / g,'')

This will get rid of anything that's not [0-9]

这将摆脱任何不是[0-9]

Edit: Here's a small function to extract all the numbers (as actual numbers) from an input string. It's not an exhaustive expression but will be a good start for anyone needing.

编辑:这是一个小函数,用于从输入字符串中提取所有数字(作为实际数字)。这不是一个详尽的表达,但对任何需要的人来说都是一个良好的开端。

function getNumbers(inputString){
    var regex=/\d+\.\d+|\.\d+|\d+/g, 
        results = [],
        n;

    while(n = regex.exec(inputString)) {
        results.push(parseFloat(n[0]));
    }

    return results;
}

var data = "123.45,34 and 57. Maybe add a 45.824 with 0.32 and .56"
console.log(getNumbers(data));
// [123.45, 34, 57, 45.824, 0.32, 0.56];

#2


26  

Not really jQuery at all:

根本不是jQuery:

number = number.replace(/\D/g, '');

That regular expression, /\D/g, matches any non-digit. Thus the call to .replace() replaces all non-digits (all of them, thanks to "g") with the empty string.

正则表达式/ \ D / g匹配任何非数字。因此,对.replace()的调用用空字符串替换所有非数字(所有这些都由于“g”)。

edit — if you want an actual *number value, you can use parseInt() after removing the non-digits from the string:

编辑 - 如果你想要一个实际的*数字值,你可以在从字符串中删除非数字后使用parseInt():

var number = "number32"; // a string
number = number.replace(/\D/g, ''); // a string of only digits, or the empty string
number = parseInt(number, 10); // now it's a numeric value

If the original string may have no digits at all, you'll get the numeric non-value NaN from parseInt in that case, which may be as good as anything.

如果原始字符串可能根本没有数字,那么在这种情况下你将从parseInt获得数值非值NaN,这可能与任何东西一样好。

#1


67  

You don't need jQuery for this - just plain old JavaScript regex replacement

你不需要jQuery - 只需简单的旧JavaScript正则表达式替换

var number = yourstring.replace(/[^0-9]/g, '')

var number = yourstring.replace(/ [^ 0-9] / g,'')

This will get rid of anything that's not [0-9]

这将摆脱任何不是[0-9]

Edit: Here's a small function to extract all the numbers (as actual numbers) from an input string. It's not an exhaustive expression but will be a good start for anyone needing.

编辑:这是一个小函数,用于从输入字符串中提取所有数字(作为实际数字)。这不是一个详尽的表达,但对任何需要的人来说都是一个良好的开端。

function getNumbers(inputString){
    var regex=/\d+\.\d+|\.\d+|\d+/g, 
        results = [],
        n;

    while(n = regex.exec(inputString)) {
        results.push(parseFloat(n[0]));
    }

    return results;
}

var data = "123.45,34 and 57. Maybe add a 45.824 with 0.32 and .56"
console.log(getNumbers(data));
// [123.45, 34, 57, 45.824, 0.32, 0.56];

#2


26  

Not really jQuery at all:

根本不是jQuery:

number = number.replace(/\D/g, '');

That regular expression, /\D/g, matches any non-digit. Thus the call to .replace() replaces all non-digits (all of them, thanks to "g") with the empty string.

正则表达式/ \ D / g匹配任何非数字。因此,对.replace()的调用用空字符串替换所有非数字(所有这些都由于“g”)。

edit — if you want an actual *number value, you can use parseInt() after removing the non-digits from the string:

编辑 - 如果你想要一个实际的*数字值,你可以在从字符串中删除非数字后使用parseInt():

var number = "number32"; // a string
number = number.replace(/\D/g, ''); // a string of only digits, or the empty string
number = parseInt(number, 10); // now it's a numeric value

If the original string may have no digits at all, you'll get the numeric non-value NaN from parseInt in that case, which may be as good as anything.

如果原始字符串可能根本没有数字,那么在这种情况下你将从parseInt获得数值非值NaN,这可能与任何东西一样好。