Does anyone know of a regular expression that would allow me to strip the path from the location.pathname of a web page?
Cheers. Scot-Bot.![]()
| SitePoint Sponsor |
Does anyone know of a regular expression that would allow me to strip the path from the location.pathname of a web page?
Cheers. Scot-Bot.![]()
Yup, here you go:
Code:<script language="JavaScript"> var re = new RegExp('/', 'i'); var str = location.pathname; var prevStr = ''; alert(re.test(str)); /* perform the Reg. Exp. once to remove the initial / from the pathname, which would be like /foo/bar */ re.exec(str); str = RegExp.rightContext; do { re.exec(str); str = RegExp.rightContext; prevStr += ('/' + RegExp.leftContext); } while (re.test(str) == true); alert(prevStr); </script>
I should probably note that those alerts are only there for me to test the script, and also for you to see what exactly it was returning. You can deal with them as you please :-) Hope I helped!
Bookmarks