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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:26:48+00:00 2026-06-09T18:26:48+00:00

Consider the following code: <!DOCTYPE html> <html> <body> <script type=text/javascript> var str = ‘<12>

  • 0

Consider the following code:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">

var str = '<12> rnbqkb-r Rnbq-b-r ';

var pat1 = new RegExp('^\\<12\\> ([rnbqkpRNBQKP-]{8}) ([rnbqkpRNBQKP-]{8})');
var pat2 = new RegExp('^\\<12\\> ([rnbqkp RNBQKP-]{8}){2}');
var pat3 = new RegExp('^\\<12\\> ([rnbqkp RNBQKP-]{8}){2}?');

document.write(str.match(pat1));
document.write('<br />');
document.write(str.match(pat2));
document.write('<br />');
document.write(str.match(pat3));

</script>
</body>
</html>

which produces

<12> rnbqkb-r Rnbq-b-r,rnbqkb-r,Rnbq-b-r
<12> rnbqkb-r Rnbq-b-, Rnbq-b-
<12> rnbqkb-r Rnbq-b-, Rnbq-b-

as output.

Why does neither pattern pat2 nor pat3 capture the first group rnbqkb-r? I would like to capture all groups without having to repeat them explicitly as in pattern pat1.

  • 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-09T18:26:49+00:00Added an answer on June 9, 2026 at 6:26 pm

    Why does neither pattern pat2 nor pat3 capture the first group rnbqkb-r?

    Because you have white-space at the end of each 8-character sequence that your regexes pat2 and pat3 do not allow.

    I would like to capture all groups without having to repeat them explicitly as in pattern pat1.

    You can’t.

    It is not possible (in JavaScript) to capture two groups when your regex only contains one group.

    Groups are defined thorugh parentheses. Your match result will contain as many groups as there are parentheses pairs in your regex (except modified parentheses like (?:...) which will not count towards match groups). Want two separate group matches in your match result? Define two separate groups in your regex.

    If a group can match multiple times, the group’s value will be whatever it matched last. All previous match occurrences for that group will be overridden by its last match.

    Try

    var pat1 = /^<12> ((?:[rnbqkp-]{8} ?)*)/i,
        match = str.match(pat1);
    
    if (match) {
      match[1].split(/\s+/);  // ["rnbqkb-r", "Rnbq-b-r", ""]
    }
    

    Notes:

    • Trim str beforehand if you don’t want the last empty array value.
    • In general, prefer regex literal notation (/expression/). Use new RegExp() only for expressions you generate from dynamic values.
    • < and > are not special, you don’t need to escape them.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
Consider the following code: $(document).ready(function() { $(body).append(<div class='outer'><span class='inner'>Click me</span></div>); $(html).click(function(event) { var targetClass
Consider the following code: index.html <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01//EN http://www.w3.org/TR/html4/strict.dtd> <html> <head>
Please consider the following code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd> <html
Consider the following code (includes jQuery): <!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://www.w3.org/TR/html4/loose.dtd>
Consider the following code: <!DOCTYPE html> <html lang=en> <head> <title>HTML</title> <meta charset=utf-8 /> <style
Consider the following rudimentary html code block: <!doctype html> <html> <head> <meta charset=utf-8> <title>iPad
Consider a page with the following code: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN
Consider a document in the following format: <!DOCTYPE html> <html> <head> <title></title> <body> <div
Consider the following code for printing questions from text file: foreach ($lines as $line_num

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.