Mokus
Mokus

Reputation: 10400

Javascript match

How could I get the numbers of this string: '(31.5393701, -82.46235569999999)'

I'm already trying something but this is far away from the solution :)

text.match(/\((\d+),(\d+)\)/);

Upvotes: 1

Views: 1273

Answers (6)

JaredMcAteer
JaredMcAteer

Reputation: 22535

"(31.5393701, -82.46235569999999)".replace(/\(|\)/g, '').split(', ');

Upvotes: 0

Chandu
Chandu

Reputation: 82933

Try string.split

e.g:

var nums = text.replace(/[^0-9-,.+]/, "").split(","); 
var num1 = parseFloat(nums[0]);
var num2 = parseFloat(nums[1]);

Working Example: http://jsfiddle.net/AL3e4/

Upvotes: 3

Ryan Matthews
Ryan Matthews

Reputation: 1043

Try something like this

text.match(/\(([\d.-]+),\s+([\d.-]+)\)/);

Upvotes: 0

Kobi
Kobi

Reputation: 138087

You are pretty close, but missing the sign, the decimal digits and some spaces. If you need to capture that from a string, try this:

/\(([+-]?\d+(?:\.\d+)?)\s*,\s*([+-]?\d+(?:\.\d+)?)\)/

Upvotes: 0

Mark Pope
Mark Pope

Reputation: 11274

How about this:

text.replace("(","").replace(")").replace(" ").split(",");

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039080

var text =  '(31.5393701, -82.46235569999999)';
var matches = text.replace('(', '').replace(')', '').split(',');
if (matches.length > 1) {
    var lat = Number(matches[0]);
    var lon = Number(matches[1]);
    alert(lat + ' ' + lon);
}

Upvotes: 1

Related Questions