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?
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?
Share Improve this question edited Oct 30, 2009 at 19:49 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Jul 15, 2009 at 8:20 Max WilliamsMax Williams 32.9k34 gold badges135 silver badges204 bronze badges 1 |6 Answers
Reset to default 7You 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.
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
).
You can also specify a radix for the number using parseInt(string, radix)
var num = parseInt(name.match(/\d+/), 10);
It's as simple in JavaScript as in Ruby (or even simpler):
var num = name.match(/\d+/);
You don't need to use the RegExp "constructor" unless you're building the pattern on the fly (usually 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 second 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.
I personally think this is the best.
function parseNumber (n) {
return Number(n.replace(/[^0-9\.-]+/g,""))
}
// Examples
parseNumber('4') // 4
parseNumber('4.00') // 4
parseNumber('$4.1s2') // 4.12
parseNumber('') // 0
parseNumber(' ') // 0
parseNumber('$4.1s2.0') // NaN
I'm also dealing with the value of inputs so I made a function like this:
function parseNum(n) {
// strip all whitespace before and after
n = String(n).trim()
// if number is '' or ' ' return empty string
if (!n) return ''
// remove everything except numbers and '.'
return n.replace(/[^0-9\.]+/g,"")
}
// Examples
parseNum('ss10ss') // 10
parseNum('ss1ss0ss') // 10
parseNum('2.00') // 2.00
parseNum('$2.12') // 2.12
parseNum('$2.s0s0ss') // 2.00
parseNum('') // ""
parseNum(' ') // ""
parseNum(4) // 4
name[/\d+/]
.String#scan
is the wrong tool if you're only interested in the first match. – Jordan Running Commented Oct 25, 2017 at 20:29