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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:49:48+00:00 2026-06-13T20:49:48+00:00

If I do: A first sentence. A second sentence.match(/A\s([^\s]*)\ssentence/g) this returns: [A first sentence,

  • 0

If I do:

"A first sentence. A second sentence".match(/A\s([^\s]*)\ssentence/g)

this returns:

["A first sentence", "A second sentence"]

And if I use:

RegExp.$1

then I just get the last first parenthesis capture, so "second".

I’d like to get the array ["first","second"]. Is it possible?

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

    You’d need to run it in a loop, and build the collection.

    var str = "A first sentence. A second sentence",
        regex = /A\s([^\s]*)\ssentence/g,
        result = [],
        match;
    
    while (match = regex.exec(str)) {
        result.push(match[1]);
    }
    
    console.log(result);
    

    Because the regex is g global, it remembers the position of the last match, and starts from there the next time the regex is used. So the loop will continue until no more matches are found.

    Inside the loop, we just add the subgroup to the result Array.


    Some people like to use .replace for this purpose.

    var str = "A first sentence. A second sentence",
        regex = /A\s([^\s]*)\ssentence/g,
        result = [];
    
    str.replace(regex, function(str, g1) {
        result.push(g1);
    });
    
    console.log(result);
    

    We’re not actually doing a string replacement, but rather are just taking advantage of the fact that it repeats the search until no more matches are found.

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

Sidebar

Related Questions

This is a textarea. The user can write anything. <textarea id=text>First sentence. Second sentence?
I need an efficient function that extracts first second and rest of the sentence
Let us suppose I have the following paragraph: This is the first sentence. This
How do i make the first sentence in the last paragraph bold using css?
I have the following string: string text = 1. This is first sentence. 2.
Need to match the first part of a sentence, up to a given word.
I'm struggling with the problem to cut the very first sentence from the string.
I wish to capitalize the first letter of a sentence, on-the-fly, as the user
I want to replace only the first occurrence of a word in a sentence
First of all, this isn't for a keylogger, it's for an input in a

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.