21

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.

1

4 Answers 4

48

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.

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

Sign up to request clarification or add additional context in comments.

5 Comments

var regex = /[+-]?\d+\.*\d+/g; will fetch both integers and floats
@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, 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.
this is a dangerous function - if the regex doesnt match, it fails with a reference error. It must always return an array, empty if no match. Fixed: const extractNumbers = str => str.match(/[+-]?\d+(\.\d+)?/g)?.map((v => parseFloat(v))) || [] I appreciate it's an old answer ofc, just pointing this out. +1
I added a runnable snippet of the code presented which clearly shows the first example logs the 1 on value1= name as a value which is not proper within the results
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.

Comments

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.

2 Comments

The format can be either numbers in xml or just numbers. I don't want to restrict the format just to xml.
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.
1

In case you have a string with a list of floats, you can convert it with this snippet:

var dataList = "2.1, 3.1, -4.5"
var dataListFloats = dataList.match(/[-+]?\d+(?:\.\d+)?/g).map(Number)

This will contain both negative and positive floats within the array.

Adapting to the initial use case:

var str = '<tag value="20.434" value1="-12.334" />'
var dataListFloats = str.match(/[-+]?\d+(?:\.)\d+/g).map(Number)

Both these as a runnable snippet with logs to demonstrate:

const dataList = "2.1, 3.1, -4.5";
const dataListFloatsCSV = dataList
  .match(/[-+]?\d+(?:\.\d+)?/g).map(Number);
console.log('C:', dataList, dataListFloatsCSV);

const str = '<tag value="20.434" value1="-12.334" />';
const dataListFloatsXML = str
  .match(/[-+]?\d+(?:\.)\d+/g).map(Number);

console.log('X:', str, dataListFloatsXML);

4 Comments

This seems to not address the first test case of <tag value="20.434" value1="-12.334" /> here, perhaps update to show that specifically
For that specific use case of strings with numerical values you could use: str.match(/[-+]?\d+(?:\.)\d+/g).map(Number)
I adjusted the answer to use single quotes on the XML to be proper syntax and added a snippet to show the results
In this case is a better result than the original answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.