-6

Let's say I have a string form-step-45. How to omit the form-step- and retrieve only the number using jQuery? The number varies and can be from 1 to ∞.

Rafff
  • 1,510
  • 3
  • 19
  • 38
  • 4
    Do you mean using JavaScript? There's no need to use jQuery for that. Is the `form-step-` constant? – BenM Oct 20 '14 at 15:19
  • 3
    What's a likely and realistic upper bound for that number? If they get too large you're going to run into issues storing it in memory. – Anthony Grist Oct 20 '14 at 15:19
  • 1
    Have you tried anything? – j08691 Oct 20 '14 at 15:19
  • possible duplicate of [How do I split a string, breaking at a particular character?](http://stackoverflow.com/questions/96428/how-do-i-split-a-string-breaking-at-a-particular-character) – Abhi Oct 20 '14 at 15:21
  • this question sure shows a complete lack of research effort – charlietfl Oct 20 '14 at 15:26

5 Answers5

1

You can use match

 var num = "your-string-with-number-45".match(/\d+/)
Y.Puzyrenko
  • 2,166
  • 1
  • 15
  • 23
1
var str = "form-step-45" ;
str = str.replace("form-step-" , "");
0

Try:

var final = 'form-step-45'.split('-');
final = final[2];
DrRoach
  • 1,320
  • 9
  • 16
0

This should do it:

    var k = 'form-step-45';
    var num = k.split('-').pop();    

    var k = 'form-step-45';
    var num = k.split('-').pop();
    alert( num );
PeterKA
  • 24,158
  • 5
  • 26
  • 48
-1

try this

var str = "form-step-45" ;
str = str.replace("form-step-" , "");
Hossam Aldeen Ahmed
  • 772
  • 1
  • 8
  • 20