Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 280293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:06:22+00:00 2026-05-12T05:06:22+00:00

I want to pull a number out the middle of a string in JavaScript

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:06:23+00:00Added an answer on May 12, 2026 at 5:06 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 180k
  • Answers 180k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I just encountered the same problem and was able to… May 12, 2026 at 4:04 pm
  • Editorial Team
    Editorial Team added an answer Action a = new Action(d); May 12, 2026 at 4:04 pm
  • Editorial Team
    Editorial Team added an answer The two defalias usages are slightly different. The first links… May 12, 2026 at 4:04 pm

Related Questions

I have a few projects coming up that have a number of endpoints or
What is the "rails way" to efficiently grab all rows of a parent table
I have a web facing, anonymously accessible, blog directory and blogs and I would
edit: I need advice on best way to search with regex in vim and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.