As a string with a number in the end convert to number?

Is it possible 1-2 operations to make the conversion abc12 12?
March 12th 20 at 08:41
2 answers
March 12th 20 at 08:43
Solution
+str.match(/\d+$/).pop()
Only if the numbers, there will be an error - noel commented on March 12th 20 at 08:46
Of course he will.

To make the not - trivial. I hope the author of the question, and he thought. And if not - can ask. If at all necessary. After all, the issue there is no word on that, and will attempt to convert strings without numbers on the end, and if so, what should be the result, but here different options are possible: return a default value - numeric, if nothing terrible in the absence of the data is non-numeric - to let them know that the data is not found; error - you can leave as is, and you can throw your own exception. - Andreane.Gislason commented on March 12th 20 at 08:49
@Andreane.Gislason, well, what is difficult to see the attraction ?????? - iliana commented on March 12th 20 at 08:52
Easy. But guess no one for whom there is no obligation, so what is the question - is the answer. - Andreane.Gislason commented on March 12th 20 at 08:55
@Andreane.Gislason, it was necessary to add the sarcasm tag ) - iliana commented on March 12th 20 at 08:58
March 12th 20 at 08:45
Solution
Then, too, so:
+str.match(/\d+$/)
If not, it will be 0.
And no there will be no error.

Find more questions by tags JavaScript