开发者

JavaScript - Checking string

开发者 https://www.devze.com 2023-02-19 09:28 出处:网络
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-desire

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-desire

var 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()
0

精彩评论

暂无评论...
验证码 换一张
取 消