I have xml content in textarea which can be of the form,
<tag value="20.434" value1="-12.334" />
Or
20.434 -12.334
I want to be able to extract the two floating numbers per line.
I have xml content in textarea which can be of the form,
<tag value="20.434" value1="-12.334" />
Or
20.434 -12.334
I want to be able to extract the two floating numbers per line.
You can use the regex /[+-]?\d+(\.\d+)?/g
in conjunction with String.match()
to parse the numbers and Array.map()
to turn them into floats:
var regex = /[+-]?\d+(\.\d+)?/g;
var str = '<tag value="20.434" value1="-12.334" />';
var floats = str.match(regex).map(function(v) { return parseFloat(v); });
console.log(floats);
var str2 = '20.434 -12.334';
var floats2 = str2.match(regex).map(function(v) { return parseFloat(v); });
console.log(floats2);
var strWithInt = "200px";
var ints = strWithInt.match(regex).map(function(v) { return parseFloat(v); });
console.log(ints);
You can always load the string into jQuery and get the attributes:
$('<tag value="20.434" value1="-12.334" />').attr('value')
$('<tag value="20.434" value1="-12.334" />').attr('value1')
In your case regex is probably the better route.
You're going to use parseFloat once you figure out how to extract the numbers from between your text... How you extract those numbers depends entirely on what that text is. Most likely you'll split the entire text on a \s and then remove all the characters that aren't numbers and dots. That should leave you with the floats... though again, impossible to say without seeing what the surrounding text looks like.
EDIT: Ok now that you've changed your question the answer is that you'll simply grab the attributes named value and value1 and run parse float on what's in them. If your text area contains this XML you'll need to parse the XML first to get the attributes as objects.