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

  • SEARCH
  • Home
  • 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 8483315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:04:04+00:00 2026-06-10T20:04:04+00:00

I am trying to capture the bold part of strings like this: ‘capture a

  • 0

I am trying to capture the bold part of strings like this:

  1. ‘capture a year range at the end of a string 1995-2010‘

  2. ‘if there’s no year range just capture the single year 2005‘

  3. ‘capture a year/year range followed by a parenthesis, including the parenthesis 2007-2012 (58 months)‘

This regex works for 1 and 2, but I can’t get it to work for 3:

/(\d+([-–— ]\d+( \(\d+ months\))?)?$)/

What am I doing wrong?

  • 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-06-10T20:04:06+00:00Added an answer on June 10, 2026 at 8:04 pm

    Try this regex:

    /\d{4}(?:[-–— ]\d{4})?(?:\s*\([^)]+\))?$/gm
    

    This one captures everything in the brackets.
    If you need a regex specific to the text “(number) months” in the brackets, then you can use this: \d{4}(?:[-–— ]\d{4})?(?:\s+\(\d+\smonths\))?$

    Link to test: RegexPal or RegExr

    Sample text:

    • capture a year range at the end of a string 1995-2010
    • if there’s no year range just capture the single year 2005
    • capture a year/year range followed by a parenthesis, including the
      parenthesis 2007-2012 (58 months)
    • trying out another example 1990 (23 weeks)
    • trying out another example 1995-2002 (x days)
    • trying out another example 2050 (blah blah)
    • trying out another example 2050—3000
    • trying out another example 2050-3000
    • trying out another example 2050–3000

    And the JavaScript code:

    var regex = /\d{4}(?:[-–— ]\d{4})?(?:\s*\([^)]+\))?$/gm; //multiline enabled
    var input = "your input string"; 
    if(regex.test(input)) {
      var matches = input.match(regex);
      for(var match in matches) {
        alert(matches[match]);
      } 
    } else {
      alert("No matches found!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to capture sub-strings from a string that looks similar to 'some
i am trying to capture raw buffer data into an audio file. Like this:
I'm trying to capture dvdauthor's output to a file. So far i found this
I'm trying to capture the ENTER event of a TextInput like so: a_txt.addEventListener(fl.events.ComponentEvent.ENTER, aEnter);
I'm trying to capture audio, using the method in this question ; with AVCaptureSession
I'm trying to capture aa,bb,cc from the following strings: ,aa,bb,cc, aa,bb,cc, ,aa,bb,cc aa,bb,cc My
i'm trying to capture/play audio with java in linux with pulseaudio server. there's no
I am trying to capture the duration elapsed between touch start and end, and
I'm trying to capture a url like the following in an intent: https://example.com/apple/orange?key=value I've
I'm trying to capture some statistics like number of 302's and 200 Oks or

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.