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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:53:32+00:00 2026-05-15T12:53:32+00:00

<html><body><script> var matches = /(\w+)(\s*(\w+))?/.exec(aaa); alert(matches.length); alert(typeof(matches[3])); </script></body><html> I’m really new to regular expressions,

  • 0
<html><body><script>
var matches = /(\w+)(\s*(\w+))?/.exec("aaa");
alert(matches.length);
alert(typeof(matches[3]));
</script></body><html>

I’m really new to regular expressions, so this may be a very easy question.

The regular expression above /(\w+)(\s*(\w+))?/ matches patterns like “aaa”, “123”, “my_var” or “aaa bbb”, “123 456”, “my_var my_value”.

For an expression like “aaa bbb”, matches = ["aaa bbb", "aaa", " bbb", "bbb"], but for an expression like “aaa”, matches = ["aaa", "aaa", ???, ???]

The first thing that surprised me is that matches.length = 4. I was expecting it to be 2, but I don’t see any document explaining what it should be. How does it work?

And the second thing that surprised me is that the 2 “extra” matches that I got are working different in the 2 browsers I’ve tested this into:

  • In Firefox 3.6.3, matches[2] and matches[3] are undefined.

  • In Internet Explorer 6, matches[2] and matches[3] are an empty string.

Basically, how should I check if I’ve got a “short” (like “aaa”) or a “long” (like “aaa bbb”) expression?

  • 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-05-15T12:53:33+00:00Added an answer on May 15, 2026 at 12:53 pm

    The standard (ECMAScript 5) is pretty clear. The length should be 4, and IE is wrong (shocking, I know).

    From §15.10.2.1, “NcapturingParens is the total number of left capturing parentheses.” You have 3.

    “A State is an ordered pair (endIndex, captures) where endIndex is an integer and captures is an internal array of NcapturingParens values. […] The nth element of captures is either a String that represents the value obtained by the nth set of capturing parentheses or undefined if the nth set of capturing parentheses hasn’t been reached yet.”

    §15.10.6.2, which describes exec, says:

    9 . d. i. Let r be the State result of the call to [[Match]]. […]

    12 . Let n be the length of r‘s captures array. (This is the same value as 15.10.2.1’s NCapturingParens.)

    13 . Let A be a new array created as if by the expression new Array() […]

    17 . Call the [[DefineOwnProperty]] internal method of A with arguments “length”, Property Descriptor {[[Value]]: I + 1}, and true. […]

    20 . For each integer i such that I > 0 and I ≤ n

    a. Let captureI be ith element of r‘s captures array.

    b. Call the [[DefineOwnProperty]] internal method of A with arguments ToString(i), Property Descriptor {[[Value]]: captureI, [[Writable]: true, [[Enumerable]]: true, [[Configurable]]: true}, and true.

    21 . Return A.

    So the length should definitely be 4 (3 + 1), and captures that don’t get reached (like (\s*(\w+)) in your pattern) remain undefined. Luckily, undefined and “” (empty string) are both falsy. This means that they are false when treated as a boolean. So you can work around IE’s bug by doing if(matches[2])

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

Sidebar

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.