Counting the number of backslashes in a string

First method


var text = "hardware\\printers\\Epson";
var paths = text.split("\\");
alert(paths.length -1);



second method


var text = "hardware\\printers\\Epson";
var numBs = 0;
var index = 0;

while(index != -1) {
index = text.indexOf("\\", index);

if (index != -1) {
numBs++;
index++;
}
}

alert(numBs);