Possible Duplicate:
Javascript StartsWith
I know that I can do like ^= to see if an id starts with something, and I tried using that for this, but it didn't work... Basically, I'm retrieving the url and I want to set a class for an element for pathnames that start in a certain way...
So,
var pathname = window.location.pathname; //gives me /sub/1/train/yonks/459087
I want to make sure that for every path that starts with /sub/1, I can set a class for an element...
if(pathname ^= '/sub/1') { //this didn't work...
...
Have a look at JavaScript
substring()
method.First, lets extend the string object. Thanks to Ricardo Peres for the prototype, I think using the variable 'string' works better than 'needle' in the context of making it more readable.
Then you use it like this. Caution! Makes the code extremely readable.
You can use string.match() and a regular expression for this too:
string.match()
will return an array of matching substrings if found, otherwise null.Use stringObject.substring
A little more reusable function: