Is there a way to get the added int value in String in javaScript?

I am currently working on a project that dynamically maps the contents of a database to a table. To change the contents of a table, I want to use the dynamically generated value "string" + id.

Is there a way to get the added int value from the whole string in javaScript?

Any suggestions would be appreciated ...

Thank!!!

+3
source share
3 answers

If you know that a string will consist only of letters or non-numeric characters, you can use the regular expression:

var str = "something123"
var id = str.replace(/^[^\d]+/i, "");

, , , string . - :

var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
+2
(''+string.match(/\d+/) || '')

: string (''+). , null, || '' .

+1

You can try using regex:

/\d+$/

to get the added number

0
source

Source: https://habr.com/ru/post/1761953/


All Articles