Javascript拆分URL

Javascript拆分URL,javascript,arrays,split,Javascript,Arrays,Split,我想拆分URL的一些特定部分,以下是我到目前为止所做的 <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 = re

我想拆分URL的一些特定部分,以下是我到目前为止所做的

<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>
变量
query
这样输出;
page,2013,05,部分页面标题

我只想要
一些页面标题
部分,还需要删除连字符

因此,最终输出将是
http://www.mydomain.com/search/?q=some 页面标题

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

拆分返回一个数组,将其用作数组

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的最后一个
/

//获取url之后的部分
//get the url
                                var url = window.location;
                                //function to get the hostname and pathname
                                //var l = getLocation(url);
                                //split the pathname by /
                                var array = url.pathname.split('/');

                                //fix the url protocol and hostname for concate
                                var pathnames = window.location.protocol + "//" + window.location.host;

                                //loop to get the splited url pathname and insert the each url in specific div
                                for (i = 1; i < array.length; i++) {

                                    //concatenate the path for each loop
                                    pathnames = pathnames + '/' + array[i];

                                    //appending an ancher tag with href for path in the element with id table_panel_header
                                    $("div#table_panel_header").append(
                                            '<a href="' + pathnames + '">'
                                                    + array[i] + '</a>/');
                                }

                                //example text  seen as: Complaint_Module/master/complaint-items/

                               /* example  href will append like
                               <div id="table_panel_header">
                                <a href="http://localhost:8010/Complaint_Module">Complaint_Module</a>/
                                <a href="http://localhost:8010/Complaint_Module/master">master</a>/
                                <a href="http://localhost:8010/Complaint_Module/master/complaint-items">complaint-items</a>/
                                </div> */
var url=window.location; //函数获取主机名和路径名 //var l=getLocation(url); //将路径名拆分为/ var array=url.pathname.split('/'); //修复concate的url协议和主机名 var pathnames=window.location.protocol+“/”+window.location.host; //循环以获取拆分的url路径名,并在特定div中插入每个url 对于(i=1;i
“/”
拆分
路径名
,并存储在
查询中
。获取数组中的最后一项(
var page=query.pop();
)并删除“.html”:
page=page.replace(/\.html$/,”),然后将“-”替换为“”:
page=page.replace(/-/g,”),然后生成最后一个字符串:
var redirectpath=”http://www.mydomain.com/search/?q=“+第页我自由编辑了你的答案,只添加了连字符删除:)^你们真是太棒了;)我把这篇文章加入书签,以便稍后回来为你们投票(因为我没有声誉去投票)
//get the url
                                var url = window.location;
                                //function to get the hostname and pathname
                                //var l = getLocation(url);
                                //split the pathname by /
                                var array = url.pathname.split('/');

                                //fix the url protocol and hostname for concate
                                var pathnames = window.location.protocol + "//" + window.location.host;

                                //loop to get the splited url pathname and insert the each url in specific div
                                for (i = 1; i < array.length; i++) {

                                    //concatenate the path for each loop
                                    pathnames = pathnames + '/' + array[i];

                                    //appending an ancher tag with href for path in the element with id table_panel_header
                                    $("div#table_panel_header").append(
                                            '<a href="' + pathnames + '">'
                                                    + array[i] + '</a>/');
                                }

                                //example text  seen as: Complaint_Module/master/complaint-items/

                               /* example  href will append like
                               <div id="table_panel_header">
                                <a href="http://localhost:8010/Complaint_Module">Complaint_Module</a>/
                                <a href="http://localhost:8010/Complaint_Module/master">master</a>/
                                <a href="http://localhost:8010/Complaint_Module/master/complaint-items">complaint-items</a>/
                                </div> */