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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:49:15+00:00 2026-06-14T01:49:15+00:00

In following code: a sasas b.match(/sas/g) //returns [sas] The string actually include two sas

  • 0

In following code:

"a sasas b".match(/sas/g) //returns ["sas"]

The string actually include two sas strings, a [sas]as b and a sa[sas] b.

How can I modify RegEx to match both?

Another example:

"aaaa".match(/aa/g); //actually include [aa]aa,a[aa]a,aa[aa]

Please consider the issue in general not just above instances.

A pure RexEx solution is preferred.

  • 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-14T01:49:16+00:00Added an answer on June 14, 2026 at 1:49 am

    This significantly-improved answer owes itself to @EliGassert.

    String.prototype.match_overlap = function(re)
        {
            if (!re.global)
                re = new RegExp(re.source,
                                'g' + (re.ignoreCase ? 'i' : '')
                                    + (re.multiline  ? 'm' : ''));
            var matches = [];
            var result;
            while (result = re.exec(this))
                matches.push(result),
                re.lastIndex = result.index + 1;
            return matches.length ? matches : null;
        }
    

    @EliGassert points out that there is no need to walk through the entire string character by character; instead we can find a match anywhere (i.e. do without the anchor), and then continue one character after the index of the found match. While researching how to retrieve said index, I found that the re.lastIndex property, used by exec to keep track of where it should continue its search, is in fact settable! This works rather nicely with what we intend to do.

    The only bit needing further explanation might be the beginning. In the absence of the g flag, exec may never return null (always returning its one match, if it exists), thus possibly going into an infinite loop. Since, however, match_overlap by design seeks multiple matches, we can safely recompile any non-global RegExp as a global RegExp, importing the i and m options as well if set.

    Here is a new jsFiddle: http://jsfiddle.net/acheong87/h5MR5/.

    document.write("<pre>");
    document.write('sasas'.match_overlap(/sas/));
    document.write("\n");
    document.write('aaaa'.match_overlap(/aa/));
    document.write("\n");
    document.write('my1name2is3pilchard'.match_overlap(/[a-z]{2}[0-9][a-z]{2}/));
    document.write("</pre>");​
    

    Output:

    sas,sas
    aa,aa,aa
    my1na,me2is,is3pi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code: $string ='۱۲۳۴۵۶۷۸۹۰'; $regex ='@۱@'; preg_match_all($regex,$string,$match); var_dump($match); will output: array(1) { [0] =>
Following code I can use for storing the value of UILabel into a string.
Following code return error Can't convert String onto integer, please help subject = ['eng','Math','Sci']
following code is used to find url from a string with php. Here is
Following code is from my REPL: scala> words.zipWithIndex.filter((x:java.lang.String,index:Int)=>index%2==0) <console>:9: error: type mismatch; found :
Following code is not compiling, can anybody please help what is wrong here class
Following code is an example, I just want to know if this can be
Following code: <?php $test_array = array(); $test_array['string_index'] = data in string index; $test_array[] =
I am following this article where I can written this code in this ruby
Following code produces a nested array as a result for keys containing three items:

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.