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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:17:38+00:00 2026-06-12T20:17:38+00:00

I have grouping parentheses () and (?: ) and I need to match even

  • 0

I have grouping parentheses () and (?: ) and I need to match even if the expression within the brackets does not match. I’ve seen both | and ? used for this (i.e. (a|b|c|) and (a|b|c)?), but which should be used/is more efficient, and why?

As different JavaScript engines interpret regex differently, I’m specifically using the SpiderMonkey engine. A generalised (both language-wise and engine-wise) answer would be nice however.

Edit: A concrete example is the DuckDuckGo Frequency goodie. Why did the author choose | over ? in this case?

  • 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-12T20:17:40+00:00Added an answer on June 12, 2026 at 8:17 pm

    To check performance, see this fiddle.


    Using ? with grouping or | with empty string as an option may lead to unexpected results!

    Couple tests:

    var myString = "this is a test string"; 
    var myRegexp = /(test)?/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // returns empty string
    
    var myString = "this is a string"; 
    var myRegexp = /(test)?/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // returns empty string
    
    var myString = "this is a test string"; 
    var myRegexp = /(test|)/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // returns empty string
    
    var myString = "this is a string"; 
    var myRegexp = /(test|)/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // returns empty string
    
    var myString = "this is a test string"; 
    var myRegexp = /(test)/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // returns "test"
    

    This one ends with error:

    var myString = "this is a string"; 
    var myRegexp = /(test)/; 
    var match = myRegexp.exec(myString); 
    alert(match[0]); // error
    

    And this one might be a solution for you:

    var myString = "this is a test string"; 
    var myRegexp = /^(?:.*(test)|(?!.*test))/; 
    var match = myRegexp.exec(myString); 
    alert(match[1]); // returns "test"
    
    var myString = "this is a string"; 
    var myRegexp = /^(?:.*(test)|(?!.*test))/; 
    var match = myRegexp.exec(myString); 
    alert(match[1]); // returns undefined
    

    Test the above code with this fiddle.

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

Sidebar

Related Questions

I have 4 group of checkboxes, in which i am grouping all this 4
I have a problem with this SQL Grouping. If i have a table with
I have a linq query that is grouping by answers by QuestionGroup. I need
I have got below XSLT which working for single grouping, however I need to
tl;dr: is it possible to have grouping levels within a subreport which gets its
This Question is regarding crystal reports sub-report grouping I have around 10 sub-reports each
I have a jqGrid in grouping mode that works fine on initial load. However,
I have the following patterns and grouping application which I am refactoring in one.
I have a text variable and a grouping variable. I'd like to collapse the
I have a data frame with a grouping variable 'ID' and some values ('Value'):

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.