RegExp: Different results when using Backreference

See http://jsfiddle.net/aEEUN/

why when I use backreference vs no backrefences, I get different results?

var str = "Hello World\nHello          ";
document.write("Without backreference: <br />");
var match = str.match(/\S(?![\s\S]*\S)/);
document.write("- match.index: " + match.index + "<br />"); // index is 16 

document.write("With backreference: <br />");
var match = str.match(/(\S)(?![\s\S]*\1)/);
document.write("- match.index: " + match.index); // index is 6

Answers


The difference is that you constrain the two character to be exactly the same.

In the first regular expression there must not be any non-whitespace character following:

/\S(?![\s\S]*\S)/

This basically means to match any non-whitespace character that is not followed by any other non-whitespace character. That’s why it matches the last o that is only followed by whitespace:

"Hello World\nHello          "
                  ^ no other non-whitespace character following

But in the second regular expression there must not be that specific character following that was matched before:

/(\S)(?![\s\S]*\1)/

This basically means to match any non-whitespace character that does not appear again in the rest of the string. That’s why the W is matched as it’s the first non-whitespace character that does not appear again after the first occurrence:

"Hello World\nHello          "
       ^ no other “W” following

That’s why it’s called a backreference: You reference a previously matched string.


Need Your Help

How to render nested views in Backbone.js?

javascript backbone.js

I have a tree of categories and I would like to do rending using by Backbone.js instead of using jQuery or do rendering on the server-side. I have the following breakout that I described as templat...

LinkedListNode after add to LinkedList doesn't set next and previous fields

c#

I'm new to C# and am trying to use LinkedList&lt;T&gt;