JavaScript - Checking string
Hi
I want to check if every words of the current URL path is match with my expected string. The current URL http://www.example.com/category/product/htc-desirevar path = location.pathname;
the path must match every words of开发者_开发问答 "/category/product/htc-desire"
How can I do it, thanks.
var path = location.path;
alert("/category/product/htc-desire" === path.replace("http://www.example.com", ""));
//should be true
Or am I missing the point?
I think it should be var path = location.pathname
.
path.indexOf(pattern) === 0
matches all paths starting with the pattern.
However, with url's, you'll also want to be case insensitive. In that case:
path.toLowerCase().indexOf(pattern.toLowerCase()) === 0
If you want an exact match:
path.toLowerCase() === pattern.toLowerCase()
精彩评论