views:

410

answers:

5

I want to pull a number out the middle of a string in JavaScript. In Ruby (my main language) I would do this:

Ruby:

name = "users[107][teacher_type]"

num = name.scan(/\d+/).first

But in JavaScript I have to do this, which seems a bit clunky.

JavaScript:

var name = "users[107][teacher_type]"

var regexp = new RegExp(/\d+/)

var num = regexp.exec(name)[0]

Is there way to pull out the matching parts without building a RegExp object? I.e. a one-liner equivalent of Ruby's String#scan?

Also, as a side note, since this string will always have the same format, I could potentially do it using .replace. This isn't as clever a solution but again I have problems in JavaScript.

In Ruby:

num = name.gsub(/users\[|\]\[teacher_type\]/,"")

But when I try this in JavaScript it doesn't like the or (|) in the middle of the regex:

In JavaScript:

//works

num = name.replace(/users\[/, "").replace(/\]\[teacher_type\]/,"")

//doesn't work

num = name.gsub(/users\[|\]\[teacher_type\]/,"")

Can anyone set me straight?

A: 

It's as simple in Javascript as in Ruby (or even simpler):

var num = name.match(/\d+/);
Philippe Leybaert
A: 

You can also specify a radix for the number using parseInt(string, radix)

var num = parseInt(name.match(/\d+/), 10);
Russ Cam
A: 

You don't need to use the RegExp "constructor" unless you're building the pattern on the fly (usu via string concatenation). Pattern literals are valid (in fact, you posted one in your snippet, when RegExp() actually prefers a string)

/\d+/.exec( name )[0]

is perfectly valid.

As to the 2nd part of your question, I think you just have a typo. In place of replace you still have gsub which is the ruby method, not a JavaScript one. The pattern itself should work just fine.

Peter Bailey
+1  A: 

You only need to use the new RegExp() part when creating dynamic regular expressions. You can use literals at other times. /\d+/ is the equivalent of new RegExp("\\d+"). Note that you have to escape special characters when using the latter.

Also noteworthy is that String#match returns null or an array. This is not apparent based on the supplied solutions (parseInt(name.match(/\d+/), 10)). It just so happens that it is converted to a string when passed to parseInt. ParseInt converts string values to integers (when possible).

name.match(/\d+/)[0]
/\d+/.exec(name)[0]

Those two are functionally identical in this case.

The other match you were referring to (the negative matching) requires a special flag. To duplicate the functionality of gsub you need to tell the regex to be applied more than once with the g flag.

'users[107][teacher_type]'.replace(/users\[|\]\[teacher_type\]/g,'')

Or if you had to use new RegExp for some reason you'd accomplish the same as above like so:

'users[107][teacher_type]'.replace(new RegExp('users\[|\]\[teacher_type\]', 'g'),'')

Notice again how I had to escape all the backslashes. Mozilla's Developer Center is a good reference to familiarize yourself with regex in javascript.

fearphage
A: 
var num = name.replace(/\D+/, '');

Be aware, though, that this one-liner does not validate the name format. It just strips out all non-digits (\D being the opposite of \d).

Geert