I have a string in javascript like this:
some string 27
or it could be
some string
or
string 123
or even
string 23A – or – string 23 A
So in other words, it’s always a string. Sometimes it ends with a number, sometimes not. And sometimes the number has a letter (only one) at the end.
Now the problem is, that I have to split this string. I need the string and number (including the letter if there’s one) stored in two variables. Like in:
some string 27
var str = “some string”
var number = 27;
I probably need to do this with regex. But i have no idea how.
Can someone please help me out with this?
You should use this regex:
Then use:
Assuming s is your variable containing original text;