JavaScript的拆分网址 [英] Javascript Split URL

查看:181
本文介绍了JavaScript的拆分网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拆分的URL的某些特定部位,这里是我到目前为止所。

I want to SPLIT some specific parts of the URL, here is what i have so far.

<script type='text/javascript'>
var query = window.location.pathname.split( '/' );
query = window.location.pathname.split( '.html' );

var redirectpath = "http://www.mydomain.com/search/?q="
window.location.href = redirectpath + query;
</script>

的URL结构是这样的:

http://www.mydomain.com/page/2013/05/some-page-title.html

变量这样的查询输出;
页,2013,05,一些页面标题

我只希望部分页面标题部分,同时删除连字符。

i only want the some-page-title part and also remove the hyphens.

所以最后的结果将是 http://www.mydomain.com/search/?q=some页面标题

怎么可能?请帮忙!!
谢谢

how is that possible? Please help!! Thanks

推荐答案

分割返回数组,使用它作为一个数组中!

Split returns an array, use it as an array!

var parts = window.location.pathname.split( '/' );
var query = parts[parts.length-1].split( '.html' );

query[0]= query[0].replace(/-/g," ");   

var redirectpath = "http://www.mydomain.com/search/?q="
window.location.href = redirectpath + query[0];

此假设你总是希望在URL后的部分最后 /

This assuming you always want the part of the url after the last /

这篇关于JavaScript的拆分网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆