我有一个 JavaScript 脚本,需要帮助修改...
我希望提取 URL 部分并将其设置为 html 元素的类。
var loc = window.location.pathname.match(REGEX_CODE_GOES_HERE);
// document.documentElement is the html element, this adds the class
if(loc) document.documentElement.className += " " + loc[1].toLowerCase();
最困难的部分是正则表达式;它应该是这样工作的:
示例网址:
http://www.somesite.com/Lists/Pages/ViewPage.aspx?ID=12
应该返回(当然,作为 html 元素的类):
lists pages viewpage aspx id 12
随意编辑代码,以您认为合适的方式......
提前致谢!
最佳答案
类似的东西
var split = window.location.pathname.split(/\/|\?|&|=|\./g);
或者如果您想将所有内容都放在一个字符串中:
var classes = window.location.pathname.toLowerCase().replace(/\/|\?|&|=|\./g," ")
可能会做你想做的事情
关于javascript - 用于提取 URL 部分的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4276101/