单个regexp获取页面URL,但从完整URL中排除端口号
Single regexp to get page URL but exclude port number from a full URL
我正试图想出一个regexp来从完整的URL中获取页面URL,但从中排除一个可能的端口号。到目前为止,我想出了以下JS:
var res = url.match(/^.*':'/'/(?:www2?.)?([^?#]+)/i);
if(res)
{
var pageURL = res[1];
console.log(pageURL);
}
如果我这样称呼它:
var url = "http://www.example.com/php/page.php?what=sw#print";
我得到了正确的答案:example.com/php/page.php
但如果我这样做:
var url = "http://www.example.com:80/php/page.php?what=sw#print";
我需要它返回example.com/php/page.php
而不是example.com:80/php/page.php
。
我可以用第二个regexp删除它,但我很好奇是否只用一个(为了速度)?
您可以将正则表达式修改为:
/^.*':'/'/(?:www2?.)?([^/:]+)(?:[^:]*:'d+)?([^?#]+)/i
RegEx演示
它将返回2个匹配项:
1: example.com
2: /php/page.php
分别作为两个输入的match[1]
和match[2]
,您可以连接。
http://www.example.com/php/page.php?what=sw#print
或
http://www.example.com:80/php/page.php?what=sw#print
更新:以下是jsperf.com上的性能结果,显示regex方法是最快的。
保持简单:
~ node
> "http://www.example.com:3000/php/page.php?what=sw#print".replace(/:'d+/, '');
'http://www.example.com/php/page.php?what=sw#print'
> "http://www.example.com/php/page.php?what=sw#print".replace(/:'d+/, '');
'http://www.example.com/php/page.php?what=sw#print'
为什么要使用正则表达式?
编辑:
正如@c00000fd所指出的:因为document
可能不可用,并且document.createElement
与RegExp相比非常慢-请参阅:
http://jsperf.com/url-parsing/5
http://jsperf.com/hostname-from-url
不过,我会留下我原来的答案,以供参考。
原始答案:
相反,您可以只使用Anchor
元素:
Fiddle:
http://jsfiddle.net/12qjqx7n/
JS:
var url = 'http://foo:bar@www.example.com:8080/php/page.php?what=sw#print'
var a = document.createElement('a');
a.href = url;
console.log(a.hash);
console.log(a.host);
console.log(a.hostname);
console.log(a.origin);
console.log(a.password);
console.log(a.pathname);
console.log(a.port);
console.log(a.protocol);
console.log(a.search);
console.log(a.username);
附加信息:
http://www.w3schools.com/jsref/dom_obj_anchor.asp
如果存在匹配端口的组,那么如何?
var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.match(/^.*':'/'/(?:www2?.)?([^?#'/:]+)(':'d+)?('/[^?#]+)/i);
if(res)
{
var pageURL = res[1]+res[3];
console.log(res, pageURL);
}
尝试
var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.split(/'w+:'/'/+'w+'.|:+'d+|'?.*/).join("");
var url = "http://www.example.com:80/php/page.php?what=sw#print";
var res = url.split(/'w+:'/'/+'w+'.|:+'d+|'?.*/).join("");
document.body.innerText = res;
您可以使用replace方法修改原始字符串或Url,
> var url = "http://www.example.com/php/page.php?what=sw#print";
undefined
> var url1 = "http://www.example.com:80/php/page.php?what=sw#print";
undefined
> url.replace(/^.*?:'/'/(?:www2?.)?([^/:]+)(?::'d+)?([^?#]+).*$/g, "$1$2")
'example.com/php/page.php'
> url1.replace(/^.*?:'/'/(?:www2?.)?([^/:]+)(?::'d+)?([^?#]+).*$/g, "$1$2")
'example.com/php/page.php'
演示
- 如何使用url加载程序在webpack中导入多个图像
- 使用php或javascript从facebook相册URL中删除多余的部分
- Angular JS IE9 Hashbang url rewriting
- JavaScript下拉菜单-点击按钮并根据所选值重定向到url
- 直接下载文件,而不是从window.open(url)
- 动态地改变“”的URL;添加新项目”;链接使用javascript/jquery
- 通过js在新选项卡中有条件地打开url
- CKFinder 3为所选文件返回错误的URL
- 如何获取不属于我项目的上一页的URL
- ReactJS映射:如何仅在url变量不为空时呈现html链接
- 在URL中传递JS对象
- 将纯文本URL转换为可单击链接
- Javascript html每点击一次就会更改url
- 如果表单中的空字段使用Javascript从URL中排除
- 如何动态更改Mozilla SDK页面mod中排除的URL
- 单个regexp获取页面URL,但从完整URL中排除端口号
- requestpage有条件地从URL中排除参数
- 如何让RegEx获取整个URL..从http开始,获取后面的所有内容,直到出现空白以及如何排除某些字符
- 获取完整的JSON请求URL以进行故障排除
- 节点表示虚荣url路径参数与排除