简体   繁体   中英

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

var path = location.pathname;
the path must match every words of "/category/product/htc-desire"

How can I do it, thanks.

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()
var path = location.path;
alert("/category/product/htc-desire" === path.replace("http://www.example.com", ""));
//should be true

Or am I missing the point?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM