javascript - How to split a string with letters and numbers into different variables using js? -
given following string:
var mystring = "s1a174o10";
i have following result:
var s = 1; var = 174; var o = 10;
each letter on string matches following number.
keep in mind string not static, here example:
var mystring = "s1p5a100";
you can regex:
var vars = {}; mystring.replace(/(\d+)(\d+)/g, function(_,k,v){ vars[k] = +v }); console.log(vars); //=> {s: 1, a: 174, o: 10}
Comments
Post a Comment