Main question is how I can update all numbers in string?
Examples where I want to update numbers in string:
-ms-transform: rotate(7deg);margin: 10px 20px 30px 40px;Value10isMoreThan5
So every "chunk" of numbers should be handled separately (e.g. 10 or 1 0 or 1px2, no matter if it's space or character between segment of numbers).
I got this working when I use spaces to split string, but it doesn't work in cases when string has no spaces rotate(7deg). Here is some small piece of "spaghetti code" example.
// Basic lerp
function lerp (min, max, amount) {
return min + amount * (max - min);
}
// Lerp values in string
function lerpAllNumbers (a, b, amount) {
var aNumbers = separateNumbers(a);
var bNumbers = separateNumbers(b);
var returnValue = null;
if (aNumbers.length === bNumbers.length) {
for (var i = 0; i < aNumbers.length; i++) {
var searchString = aNumbers[i].value + aNumbers[i].extension;
var newValue = lerp(aNumbers[i].value, bNumbers[i].value, amount).toFixed(3) + aNumbers[i].extension;
returnValue = a.toString().replace(searchString, newValue);
}
return returnValue;
}
}
// Separate numbers from string and create object with value and extension
function separateNumbers(text) {
var splitter = text.toString().split(" ");
var returnArray = [];
splitter.forEach(function(item){
var numberObj = {
value: parseFloat(item),
extension: item.replace(/^-?\d+((.|,)\d+)?/g, '')
}
returnArray.push(numberObj);
});
return returnArray;
}
Sorry if there is duplicates. I've found already useful post like this one: regex to split number from string
But none for this case where I need to update multiple numbers and put them back to string in right places after update.
Is there smarter/easier way to update all numbers in strings with JavaScript (Vanilla or jQuery)?
.replace()with a regex pattern and a custom replacement function. Docs here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…