Q:

Understanding a REGEX Quirk in using \S in JavaScript

I'm refreshing my long lost knowlege of regex in Java Script and cannot fathom this.
As I recall \S means not white space and \D means not a number. These are the counterpart to the lower case alternatives which would search for a space or a number. respectively.
Search for the first non numeric charter that does not have white space either side
Code:
var digitSurroundedBySpace = /\S\D\S/;
document.writeln("1234567AB".search(digitSurroundedBySpace));

this returns 6. Which is not what I expect. It seems the mere presence of the first \S thows the answer off by one.
Search for the first non numeric charter that does not have white space after it.
var digitSurroundedBySpace = /\D\S/;
document.writeln("1234567AB".search(digitSurroundedBySpace));

this returns 7 which I expect.
Should they not both return a 7. Why does the first one not? 

A:

You are getting confused by the characters in the string versus the index position of those characters in the string. The first match is at 7AB. The 7 is character number 6 in the string, because we start counting from 0. Thus, your .search() returns 6, the correct offset of 7AB.
var digitSurroundedBySpace = /\S\D\S/;
document.writeln("1234567AB".search(digitSurroundedBySpace));
//  index:        012345678
//  match:              ^

var digitSurroundedBySpace = /\D\S/;
document.writeln("1234567AB".search(digitSurroundedBySpace));
//  index:        012345678
//  match:               ^

