admin管理员组文章数量:1022594
I want to be able to extract all numbers (including floating point) from a string in JavaScript.
"-5. -2 3.1415 test 2.4".match(...)
returns
["-2", "3.1415", "2.4"]
So far I have /[-+]?(\d*\.?\d+)/g
, but this seems to return 5
along with the other numbers. I want to avoid that since in the string the "word" is 5. and not 5 or 5.0. Any hints?
I want to be able to extract all numbers (including floating point) from a string in JavaScript.
"-5. -2 3.1415 test 2.4".match(...)
returns
["-2", "3.1415", "2.4"]
So far I have /[-+]?(\d*\.?\d+)/g
, but this seems to return 5
along with the other numbers. I want to avoid that since in the string the "word" is 5. and not 5 or 5.0. Any hints?
-
2
What about things like
.5
? Are both sides of the decimal point mandatory if it is there? – Martin Ender Commented Nov 30, 2012 at 0:01 - -1 because your question problem statement and data do not agree. Please be consistent and precise. Also, I am "quite certain" this has been done numerous times before .. – user166390 Commented Nov 30, 2012 at 0:01
- @pst: His problem is that he doesn't want '5.' to be recognized as a valid number. It's your understanding that doesn't match the problem statement. – slebetman Commented Nov 30, 2012 at 3:16
- 'match(...) .. returns ["-2", "3.1415", "2.4"]' .. 'So far I have /[-+]?(\d*\.?\d+)/g, but this seems to return 5 along with the other numbers.' If you understand this better, consider updating the question. – user166390 Commented Nov 30, 2012 at 4:15
4 Answers
Reset to default 2If you want to not include the dot, a look-ahead would make sense:
/[-+]?\d*(\.(?=\d))?\d+/g
Another option is to move the second \d+
inside the parentheses:
/[-+]?\d+(\.\d+)?/g
This rx gets numbers represented in strings, with or without signs, decimals or exponential format-
rx=/[+-]?((.\d+)|(\d+(.\d+)?)([eE][+-]?\d+)?)/g
String.prototype.getNums= function(){
var rx=/[+-]?((\.\d+)|(\d+(\.\d+)?)([eE][+-]?\d+)?)/g,
mapN= this.match(rx) || [];
return mapN.map(Number);
};
var s= 'When it is -40 degrees outside, it doesn\'t matter that '+
'7 roses cost $14.35 and 7 submarines cost $1.435e+9.';
s.getNums();
/* returned value: (Array) -40, 7, 14.35, 7, 1435000000 */
The reason this question is difficult to answer is you need to be able to check that the character before the number or before the + or - is either a whitespace or the start of the line.
As JavaScript does not have the ability to lookbehind a solution for this by using the .match()
method on a string bees nearly impossible. In light of this here is my solution.
var extractNumbers = (function ( ) {
numRegexs = [
/( |^)([+-]?[0-9]+)( |$)/g,
/( |^)([+-]?[0-9]+\.[0-9]+)( |$)/g,
/( |^)([+-]?[0-9]+\.[0-9]+([eE][+-])?[0-9]+)( |$)/g
];
return function( str ) {
var results = [], temp;
for( var i = 0, len = numRegexs.length; i < len; i++ ) {
while( temp = numRegexs[i].exec( str ) ) {
results.push( temp[2] );
}
}
return results;
}
}( ));
The regular expressions in the numRegexs array correspond to integers, floats and exponential numbers.
Demo here
Given that your example string has spaces between the items, I would rather do:
var tokens = "-5. -2 3.1415 test 2.4".split(" ");
var numbers = [];
for( var i = 0 ; i < tokens.length ; i++ )
if( isNumber( tokens[i]) )
numbers.push( tokens[i]*1 );
//numbers array should have all numbers
//Borrowed this function from here:
//http://stackoverflow./questions/18082/validate-numbers-in-javascript-isnumeric
function isNumber(n) {
return !isNaN(parseFloat(n*1)) && isFinite(n*1) && !endsWith(n,".");
}
endsWith = function( s , suffix ) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
};
I want to be able to extract all numbers (including floating point) from a string in JavaScript.
"-5. -2 3.1415 test 2.4".match(...)
returns
["-2", "3.1415", "2.4"]
So far I have /[-+]?(\d*\.?\d+)/g
, but this seems to return 5
along with the other numbers. I want to avoid that since in the string the "word" is 5. and not 5 or 5.0. Any hints?
I want to be able to extract all numbers (including floating point) from a string in JavaScript.
"-5. -2 3.1415 test 2.4".match(...)
returns
["-2", "3.1415", "2.4"]
So far I have /[-+]?(\d*\.?\d+)/g
, but this seems to return 5
along with the other numbers. I want to avoid that since in the string the "word" is 5. and not 5 or 5.0. Any hints?
-
2
What about things like
.5
? Are both sides of the decimal point mandatory if it is there? – Martin Ender Commented Nov 30, 2012 at 0:01 - -1 because your question problem statement and data do not agree. Please be consistent and precise. Also, I am "quite certain" this has been done numerous times before .. – user166390 Commented Nov 30, 2012 at 0:01
- @pst: His problem is that he doesn't want '5.' to be recognized as a valid number. It's your understanding that doesn't match the problem statement. – slebetman Commented Nov 30, 2012 at 3:16
- 'match(...) .. returns ["-2", "3.1415", "2.4"]' .. 'So far I have /[-+]?(\d*\.?\d+)/g, but this seems to return 5 along with the other numbers.' If you understand this better, consider updating the question. – user166390 Commented Nov 30, 2012 at 4:15
4 Answers
Reset to default 2If you want to not include the dot, a look-ahead would make sense:
/[-+]?\d*(\.(?=\d))?\d+/g
Another option is to move the second \d+
inside the parentheses:
/[-+]?\d+(\.\d+)?/g
This rx gets numbers represented in strings, with or without signs, decimals or exponential format-
rx=/[+-]?((.\d+)|(\d+(.\d+)?)([eE][+-]?\d+)?)/g
String.prototype.getNums= function(){
var rx=/[+-]?((\.\d+)|(\d+(\.\d+)?)([eE][+-]?\d+)?)/g,
mapN= this.match(rx) || [];
return mapN.map(Number);
};
var s= 'When it is -40 degrees outside, it doesn\'t matter that '+
'7 roses cost $14.35 and 7 submarines cost $1.435e+9.';
s.getNums();
/* returned value: (Array) -40, 7, 14.35, 7, 1435000000 */
The reason this question is difficult to answer is you need to be able to check that the character before the number or before the + or - is either a whitespace or the start of the line.
As JavaScript does not have the ability to lookbehind a solution for this by using the .match()
method on a string bees nearly impossible. In light of this here is my solution.
var extractNumbers = (function ( ) {
numRegexs = [
/( |^)([+-]?[0-9]+)( |$)/g,
/( |^)([+-]?[0-9]+\.[0-9]+)( |$)/g,
/( |^)([+-]?[0-9]+\.[0-9]+([eE][+-])?[0-9]+)( |$)/g
];
return function( str ) {
var results = [], temp;
for( var i = 0, len = numRegexs.length; i < len; i++ ) {
while( temp = numRegexs[i].exec( str ) ) {
results.push( temp[2] );
}
}
return results;
}
}( ));
The regular expressions in the numRegexs array correspond to integers, floats and exponential numbers.
Demo here
Given that your example string has spaces between the items, I would rather do:
var tokens = "-5. -2 3.1415 test 2.4".split(" ");
var numbers = [];
for( var i = 0 ; i < tokens.length ; i++ )
if( isNumber( tokens[i]) )
numbers.push( tokens[i]*1 );
//numbers array should have all numbers
//Borrowed this function from here:
//http://stackoverflow./questions/18082/validate-numbers-in-javascript-isnumeric
function isNumber(n) {
return !isNaN(parseFloat(n*1)) && isFinite(n*1) && !endsWith(n,".");
}
endsWith = function( s , suffix ) {
return s.indexOf(suffix, s.length - suffix.length) !== -1;
};
本文标签: regexExtract all numbers from string in JavaScriptStack Overflow
版权声明:本文标题:regex - Extract all numbers from string in JavaScript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745519531a2154249.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论