Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following string in javascript:

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.Any ideas are welcomed

share|improve this question

I solved this with the following regex:

var string = "border-radius:10px 20px 30px 40px";
var numbers = string.match(/\d+/g).map(Number);
share|improve this answer
1  
This is a much better & more robust solution. I'm sure others can help you make your regex better (not my strength so I'm not commenting on that aspect). – Jaxidian Jan 5 at 16:07
3  
If you then want the numbers to be actual numbers instead of strings, you can then map it; the simplest version of this would be: string.match(/\d+/g).map(Number) – ndugger Jan 5 at 17:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.