Reputation: 9733
I'm trying to retrieve the two integers (not floating numbers) from the following string:
0 1
0 2
0 3
0 4
0 5
0 6
0 7
0 8
12 11
10 22
33 2
102 149
I want to get in javasScript like:
var line = "0 1";
var firstInt = line.someMagic();
var secondInt = line.someMagic();
please help. Any help will be appreciated. thanks!
EDIT
the split method can't be used.
cos sometimes the line has extra space in front or at the end of each line!
like:
var line = " 0 2 ";
that's why I needed the regex.
EDIT 2
Turns out, I don't need the regex!
thanks for the help!
Upvotes: 1
Views: 58
Reputation: 135197
This solution will only work for positive integers
// ES5
var input = "0 1";
var matches = input.match(/\d+/g);
var a = matches[0];
var b = matches[1];
console.log(a); // 0
console.lob(b); // 1
It's a little nicer with ES6
// ES6
let input = "0 1";
let [a,b] = input.match(/\d+/g);
console.log(a); // 0
console.lob(b); // 1
That said, RegExp isn't the only way to solve this. You may have leading or trailing space, but that's a non-issue. Here's a functional approach that makes quick work of this problem for you
const isNumber = x => ! Number.isNaN(x);
const parseInteger = x => window.parseInt(x, 10);
let input = ' 20 -54 ';
let [a,b] = input.split(' ').map(parseInteger).filter(isNumber);
console.log(a); // 20
console.lob(b); // -54
Also note some people are advising the use of .trim
which is not really going to solve your problems here. Trim may remove extraneous whitespace at the beginning end of your string, but it's not going to remove extra spaces in between the numbers. My solution above works regardless of how many spaces are used, but if you evaluate it, you'll find that it might be improved by splitting (not matching) with a regexp
// same effect as above, but breaks the string into less parts
let [a,b] = input.split(/\s+/).map(parseInteger).filter(isNumber);
The result of this is that the map
and filter
operations don't have to test for as many ''
(empty string) and NaN
(not a number) values. However, the performance cost of using the regexp engine may not outweigh the few extra cycles used to process the empty strings one-by-one.
Upvotes: 2
Reputation: 9782
You can use the split method to break the string into an array:
var line = "0 1";
var pieces = line.trim().split(" ");
console.log(pieces);
If you prefer to use a regex:
var line = "0 1";
var pieces = /(\d+)\s(\d+)/g.exec(line);
console.log(pieces);
Upvotes: 1
Reputation: 17952
Just split the line on the space, and parseInt
the chars into numbers :)
var line = "-21 42";
var ints = line.split(' ').map(function (num) {
return parseInt(num, 10);
});
var firstInt = ints[0]; // -21
var secondInt = ints[1]; // 42
If you are worried about trailing spaces etc, just trim
the string first:
var ints = line.trim().split(' ').map(...
Upvotes: 4
Reputation: 5637
You don't even need regex for this, it can be done very simply:
var line = "0 1";
// Split line into an array using the space as a delimiter
var parsedLine = line.split(" ");
var firstInt = parsedLine[0]; // => 0
var secondInt = parsedLine[1]; // => 1
Upvotes: 0