How do I extract the number inside of a string like the examples below?
myform-5-id
myform-32-id
myform-0-id
The number will always be an integer >= 0, and the text will always be the same.
How do I extract the number inside of a string like the examples below?
myform-5-id
myform-32-id
myform-0-id
The number will always be an integer >= 0, and the text will always be the same.
The regex that you are looking for is /\d+/
.
Regex Explanation:
\d+
matches one or more numbers/
is the way to mention the regex patternWorking Code Snippet:
var r = /\d+/;
var s = "myform-5-id";
alert (s.match(r));
Use parseInt();
var int= parseInt('myform-5-id'.match(/[0-9]+/), 10);
alert(int);
This is an actual number and not a string.
If you want a non regex solution than you can use this otherwise @Rahul answer is perfect
var a="myform-5-id";
var res=a.split('-');
alert(res[1]);