Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

why this function writes 2 instances of "on "?

(function myFunction()

{

    var str="on";

    var n=str.match(/^(on|off)$/i);

    document.write(n);

})()

Update:

this is #javascript of course

Update 2:

even exec does the same

Update 3:

it should write "on" not "on,on"

2 Answers

Relevance
  • Anonymous
    8 years ago
    Favorite Answer

    It's because your regex has the paretheses (). The first element of the array is the whole regex match, and the next elements represent the part that is matched inside parentheses.

  • Steven
    Lv 4
    8 years ago

    wouldn't you want to use str.length? I guess I'm assuming you want to count the letters in the string

Still have questions? Get your answers by asking now.