Split numbers from string

Hello,

I have this string:
field = “due_date31”;

is there a way to split it into two vars so it will be like:
var 1 = “due_date”;
var 2 = “31”;

any ideas?

thanks!

Is “field” always “due_date##”?

If so you could use .substring(8, 11) to get the number for your second variable (though it would still be a string “31” and not really a number until you make it one).

For the first one you could maybe match a pattern?
field=“due_date31”;
var textchars=/([\w_]+)/; //assuming you only ever have letters and underscores
var a=field.replace(textchars, $1);

Maybe easier to use matching regexes for both?


field="due_date31";
var pattern=/([\\w_]+)(\\d+)/;
var a=field.replace(pattern, $1);
var b=field.replace(pattern, $2);

I dunno what all you have, and I expect my solutions to be clunky at best : ) My pattern is assuming field is always letters/underscores followed by numbers.

no, it is not due_date always. It depends on the field that sending the request

no, it is not due_date always. It depends on the field that sending the request

If using a pattern-match is the best method (and I don’t know that it is) then the pattern var would need to be changed to fit your data set. I’d also make sure the numbers you are getting are really numbers and not “31” strings, if it matters.