URL与querystring的正则表达式
Regex for URL with querystring
我使用下面的javascript和regex来测试url字符串。
var url = window.location.pathname;
// create regexp to match current url pathname and remove trailing slash if
// present as it could collide with the link in navigation in case
// trailing slash wasn't present there
urlRegExp = new RegExp(url == '/' ? window.location.origin + '/?$' : url.replace(/'/$/, ''));
// now grab every link from the navigation
$('.page-sidebar a').each(function () {
// and test its normalized href against the url pathname regexp
if (urlRegExp.test(this.href.replace(/'/$/, ''))) {
$(this).closest("li").addClass("active");
}
});
但是这个正则表达式不包括querystring。我该怎么做呢?
也许你可以用这样的东西匹配字符串,并从中构造你想要的东西。
var rx = new RegExp("^(?:([^:''/?#]+):)?(?:''/''/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:''/?#]*)(?::(''d*))?))?((((?:[^?#''/]*''/)*)([^?#]*))(?:''?([^#]*))?(?:#(.*))?)");
var url = "http://stackoverflow.com/questions/16053753/regex-for-url-with-querystring?a0b&c=d&e=f#anchor";
var val = url.match(rx);
val.forEach(function (part) {
var result = $("#result");
var $text = $("<p>").text(part);
result.append($text);
});
您可以在jsfiddle
相关文章:
- java.net和javascript之间正则表达式的差异
- Grunt匹配正则表达式
- 不同浏览器中的空白字符正则表达式行为
- 正则表达式在字符串中找到base64
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- 调用正则表达式匹配的函数
- 使用正则表达式评估电子邮件地址时出现性能问题
- Javascript 正则表达式 : ^[^/s/]+[a-z]{1,}[0-9]*[-_]*[^/][
- JavaScript正则表达式文本与RegExp对象
- 正则表达式只允许 x 个整数
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 改进用于验证付款金额的正则表达式
- 正则表达式与数字中的第二个点匹配
- 键按正则表达式以查找具有负值的小数
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- 将po-box javascript正则表达式转换为c#regex
- 在mongoose中使用正则表达式在对象数组中进行查询搜索
- 使用正则表达式捕获“”并分割成阵列
- 将这些正则表达式从javascript转换为Java
- 用于验证JS中逻辑运算符表达式的正则表达式