How can javascript extract number from string?

310    Asked by neeraj_7796 in Java , Asked on Oct 12, 2022

 Consider the following string in [removed]

var string="border-radius:90px 20px 30px 40px";

I want to extract the 4 numbers from that string and store them in an array called numbers.To do that I developed the following code:

var numbers=string.split("border-radius:");

numbers=numbers[1].split("px");

This code is working fine but I was wondering if there is a better approach/solution.


Answered by Neetu Sharma

Your question - How can javascript extract number from string can be answered as -


I used the following regex to extract numbers from a string:var string = "border-radius:10px 20px 30px 40px";

  var numbers = string.match(/d+/g).map(Number);


Your Answer

Interviews

Parent Categories