2
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>chap07</title>
    <script>
    function make_a_matcher(){
        return /a/gi;
    }

    var x = make_a_matcher();
    var y = make_a_matcher();

    x.lastIndex = 10;

    document.writeln('<div>x.global : '+x.global+'<div>');
    document.writeln('<div>x.ignoreCase : '+x.ignoreCase+'<div>');
    document.writeln('<div>x.lastIndex : '+x.lastIndex+'<div>');
    document.writeln('<div>x.multiline : '+x.multiline+'<div>');
    document.writeln('<div>x.source : '+x.source+'<div>');

    document.writeln('<div>y.global : '+y.global+'<div>');
    document.writeln('<div>y.ignoreCase : '+y.ignoreCase+'<div>');
    document.writeln('<div>y.lastIndex : '+y.lastIndex+'<div>');
    document.writeln('<div>y.multiline : '+y.multiline+'<div>');
    document.writeln('<div>y.source : '+y.source+'<div>');
    </script>
</head>
<body>
</body>
</html>

I'm studying JavaScript with "JavaScript The Good Parts" written by Douglas Crockford. He described that RegExp objects made by regular expression literals share a single instance and above example.

The result in the book is y.lastIndex : 10. But my result is y.lastIndex : 0.

I would appreciated if you tell me the exact reason of this diffrent result.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user2952980
  • 81
  • 1
  • 5
  • 2
    I think Crockford's information is out of date. In modern browsers you'll get separate instances. (Try _console.log (x===y)_) – nnnnnn Nov 27 '13 at 02:14
  • Are you sure he's not talking about the `RegExp` function object, which does have some properties that are shared among all regex instances? – Blue Skies Nov 27 '13 at 02:26

4 Answers4

0

Crockford's info is not correct (or not current) and any simple test will verify that you get a new and different object each time you call your function.

If you want them to return the same object, you can do this:

var make_a_matcher = (function() {
    var regex = /a/gi;
    return function() {
        return regex;
    }
})();

Demo: http://jsfiddle.net/jfriend00/FaDwP/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

See this SO question and it's answers. In particular one of the answers references the ECMA-262 spec, §7.8.5 Regular Expression Literals, which states that:

A regular expression literal is an input element that is converted to a RegExp object (see 15.10) each time the literal is evaluated.

So each time the function make_a_matcher is called a new regexp object is created. It seems Mr Crockford's book might be out of date if it states otherwise.

Community
  • 1
  • 1
harmic
  • 28,606
  • 5
  • 67
  • 91
0

When you do this,

function make_a_matcher(){
  return /a/gi;
}

var x = make_a_matcher();
var y = make_a_matcher();

you are creating separate instances. It's the same as:

function make_a_matcher(){
  return new RegExp('a', 'gi');
}

If you create the instance before you create make_a_matcher(), it should work,

var agi = /a/gi;
function make_a_matcher(){
  return agi;
}

although there would be no need since you could just use that variable.

StackSlave
  • 10,613
  • 2
  • 18
  • 35
0

In my computer, y is also equal to 0; Because make_a_matcher function return an object called RegExp object, and x, y are different instances of RegExp object, which means that x, y are separate!

function make_a_matcher() {
return /a/gi;
}
var x = make_a_matcher();
var y = make_a_matcher();
x.lastIndex = 10;
console.log(y.lastIndex);

enter image description here

In the follow picture we can see that x, y are different instances of RegExp, but not equal. enter image description here

boyden
  • 113
  • 1
  • 1
  • 9