Check if string begins with something? [duplicate]
Solution 1:
Use stringObject.substring
if (pathname.substring(0, 6) == "/sub/1") {
// ...
}
Solution 2:
String.prototype.startsWith = function(needle)
{
return this.indexOf(needle) === 0;
};
Solution 3:
You can use string.match() and a regular expression for this too:
if(pathname.match(/^\/sub\/1/)) { // you need to escape the slashes
string.match()
will return an array of matching substrings if found, otherwise null.