How to output a number from a string in JavaScript?

I want to print the number from the middle of the string in JavaScript . In Ruby (my main language) I would do the following:

Ruby:

name = "users[107][teacher_type]" num = name.scan(/\d+/).first 

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

JavaScript:

 var name = "users[107][teacher_type]" var regexp = new RegExp(/\d+/) var num = regexp.exec(name)[0] 

Is there a way to pull the relevant parts without creating a RegExp object? That is, the one-line equivalent of a Ruby String # scan string?

Also, as a side note, since this line will always have the same format, I could do this with .replace. This is not such a smart solution, but again I have problems with JavaScript.

In Ruby:

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

But when I try this in JavaScript, I don't like it or (|) in the middle of the regular expression:

In JavaScript:

 //works num = name.replace(/users\[/, "").replace(/\]\[teacher_type\]/,"") //doesn't work num = name.gsub(/users\[|\]\[teacher_type\]/,"") 

Can anyone set me straight?

+6
javascript regex
source share
6 answers

You need to use the new RegExp() when creating dynamic regular expressions. You can use literals at other times. / \ d + / is the equivalent of new RegExp("\\d+") . Please note that when using the latter you should avoid special characters.

It should also be noted that String#match returns null or an array. This is not obvious based on the supplied solutions ( parseInt(name.match(/\d+/), 10) ). It so happened that it is converted to a string when passed to parseInt . ParseInt converts string values ​​to integers (if possible).

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

The two are functionally identical in this case.

In another match you were referring to (negative match), a special flag is required. To duplicate gsub functionality, you need to specify a regular expression to apply 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 would have done the same thing as above:

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

Remember once again how I had to avoid all backslashes. The Mozilla Developer Center is a good reference to familiarize yourself with the regex in javascript.

+7
source share
 var num = name.replace(/\D+/, ''); 

Remember that this single line file does not confirm the format of the name. It just removes all non-digital characters ( \D is the opposite of \D ).

+3
source share

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

 var num = parseInt(name.match(/\d+/), 10); 
+2
source share

It is as simple in JavaScript as it is in Ruby (or even simpler):

 var num = name.match(/\d+/); 
+1
source share

You do not need to use the RegExp constructor if you do not create the template on the fly (usually using string concatenation). The sample patterns are valid (in fact, you placed one in your fragment, when RegExp () actually prefers the string)

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

fine.

Regarding the second part of your question, I think you just have a typo. Instead of replace , you still have gsub , which is a Ruby method, not JavaScript. The model itself should work fine.

+1
source share

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 also deal with the value of the 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 
0
source share

All Articles