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 9037781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:19:33+00:00 2026-06-16T09:19:33+00:00

I have a long regex that is generated to match URLs like /^\/([^\/.?]+)(?:\/([^\/.?]+)(?:\/([^\/.?]+)(?:\.([^\/.?]+))?)?)?$/ Would

  • 0

I have a long regex that is generated to match URLs like

/^\/([^\/.?]+)(?:\/([^\/.?]+)(?:\/([^\/.?]+)(?:\.([^\/.?]+))?)?)?$/

Would match:

/foo/bar/1.html

as ['foo', 'bar', '1', 'html']

In Javascript I would like to get the parts that match as the user types the url (like a typeahead). For example if they typed:

/foo

It would tell me that /foo was matched, but the whole regexp hasn’t been satisfied. Ruby can return an array with only the matching partial elements like : ['foo', nil, nil, nil] is this possible, or easy to do in Javascript?

  • 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-16T09:19:34+00:00Added an answer on June 16, 2026 at 9:19 am

    @minitech basically gave half the answer: use ? after each group, and then you’ll be able to match the regex even if they’re missing. Once you can do that, then just check the groups of the regex result to see which bits have been matched and which haven’t.

    For example:

    /^\/([^\/.?]+)?(?:\/([^\/.?]+)?(?:\/([^\/.?]+)?(?:\.([^\/.?]+))?)?)?$/.exec('/ab/c')
    

    Would return:

    ["/ab:c", "ab:c", "c", undefined, undefined]
    

    By checking and seeing that the fourth value returned is undefined, you could then figure out which chunks were/were not entered.

    As a side note, if you’re going to be working lots of regexs like this, you can easily lose your sanity just trying to keep track of which group is which. For this reason I strongly recommend using “named group” regular expressions. These are otherwise normal regular expressions that you can create if you use the XRegxp library (http://xregexp.com/), like so:

    var result = XRegExp.exec('/ab/c', /^\/(?<fooPart>[^\/.?]+)?(?<barPart>?:\/([^\/.?]+)?(?:\/([^\/.?]+)?(?:\.([^\/.?]+))?)?)?$/)
    var fooPart = result.fooPart
    

    That library also has other handy features like comments that can similarly help keep regular expression under control. If you’re only using this one regex it’s probably overkill, but if you’re doing lots of JS regexp work I can’t recommend that library enough.

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

Sidebar

Related Questions

I have a text file that is like this: FOO BAR PIPPO PLUTO 31337
I have a long string that contains multiple css classes. With regex I would
I have the following regex: ^[a-zA-Z](.*)[a-zA-Z]$ on both the Javascript and PHP side that
Is this actually doable? I have some very long regex pattern rules that are
I have a regex that is going to end up being a bit long
I have a regex that looks like this ((1010xxx)?(\d{11}|\d{10}|\d{7})+) Basically I want it to
Long story short, I have two regex patterns. One pattern matches things that I
I have a long RegEx that is working, but I have a section at
I have the following regex (long, I know): (?-mix:((?-mix:(?-mix:\{\%).*?(?-mix:\%\})|(?-mix:\{\{).*?(?-mix:\}\}?)) |(?-mix:\{\{|\{\%))) that I'm using to
I have another Javascript regex conundrum... (apologies if this is a bit long-winded -

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.