-3

I have a string called

var string ="Catch 22 Slaughterhouse 5 Fahrenheit 451"

I just want to print out the numbers from the string using the split function, array or loops in javascript

Satpal
  • 132,252
  • 13
  • 159
  • 168
Juliet
  • 11
  • 4
  • Please take the [tour], have a look around, and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) – T.J. Crowder Jul 27 '15 at 06:38

2 Answers2

0

You can use regular expression to extract numbers from the string.

\d will match any number from the string. + will match one or more of the preceding characters.

Demo

var str = "Catch 22 Slaughterhouse 5 Fahrenheit 451";

var numbers = str.match(/\d+/g);

document.write(numbers);
Tushar
  • 85,780
  • 21
  • 159
  • 179
0

you could try,

string.match(/\d+/g);

it returns an array of numbers matched.

Ace
  • 700
  • 7
  • 37