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);
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);
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;.var regex = /[+-]?\d*\.*\d+/g; but I did not check for the 1......1 possibility. Yours is perfect, thanks a lot.const extractNumbers = str => str.match(/[+-]?\d+(\.\d+)?/g)?.map((v => parseFloat(v))) || [] I appreciate it's an old answer ofc, just pointing this out. +1value1= name as a value which is not proper within the resultsYou'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.
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);
<tag value="20.434" value1="-12.334" /> here, perhaps update to show that specificallystr.match(/[-+]?\d+(?:\.)\d+/g).map(Number)