19

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.

gizgok
  • 7,303
  • 21
  • 79
  • 124
  • This question seems to valid since I can't answer here so posting a link to answer that helped me https://stackoverflow.com/a/34311427/6517383 – Black Mamba Apr 29 '20 at 15:52

3 Answers3

45

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);

See demo code here.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • var regex = /[+-]?\d+\.*\d+/g; will fetch both integers and floats – Jayaram Jun 25 '14 at 14:09
  • 2
    @JayaramRPai That's nice, but has some minor problems: it would not match something simple, such as `1` and would match stuff like `1......1`. For both integers and floats, I'd suggest adapting it to something like `var regex = /[+-]?\d+(\.\d+)?/g;`. – acdcjunior Jun 25 '14 at 16:35
  • 1
    @acdcjunior, I had slightly modified my regex to `var regex = /[+-]?\d*\.*\d+/g;` but I did not check for the `1......1` possibility. Yours is perfect, thanks a lot. – Jayaram Jun 26 '14 at 06:38
2

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.

Mataniko
  • 2,212
  • 16
  • 18
1

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.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • The format can be either numbers in xml or just numbers. I don't want to restrict the format just to xml. – gizgok Jun 28 '13 at 23:09
  • I'm not even sure what the question is... just run parseFloat on everything that might contain floats, check for isNaN() and if it's false then you have a float. – Yevgeny Simkin Jun 28 '13 at 23:10