【问题标题】:Using javascript to join the different parts of a url plus add a delimiter except on end of last array variable使用 javascript 加入 url 的不同部分,并在最后一个数组变量的末尾添加分隔符
【发布时间】:2016-08-29 21:36:20
【问题描述】:

尝试加入 url 的不同部分并为“:”(冒号空格)添加分隔符,该分隔符不会添加到可能更改的最后一个数组值。这用于网络分析数据层。

我尝试使用下面的方法,但如果我有一个小于 4 的数组,它将不起作用,并将分隔符添加到最后一个变量的末尾。

输入:

var pathArray = window.location.pathname.split( '/' );
var siteDomain = hostName; // returns the URL hostname of the current page.
var siteSection2 = pathArray[1]; /// returns the second-level directory path of the current page.
var siteSection3 = pathArray[2]; // returns the third-level directory path of the current page.
var siteSection4 = pathArray[3]; // returns the fourth-level directory path of the current page.

第一次尝试 - 尝试了以下方法,但它删除了最后一条路径 (pathArray[3])

var pageNameString = [hostName, pathArray[1], pathArray[2], pathArray[3]];
var pageName = pageNameString.slice(0, -1).join(': ');

第二次尝试 - 如果路径小于 4,仍将分隔符添加到末尾

var pageNameString = [hostName, pathArray[1], pathArray[2], pathArray[3]];
var pageName = pageNameString.slice(0, 4).join(': ');

第三次尝试 - 仍将分隔符添加到末尾

var pageName = [hostName, pathArray[1], pathArray[2], pathArray[3]].join(': ');

所需的输出:

  • 当 1 个路径深 (hostName) 时,它应该返回:“hostname”
  • 当 2 个路径深 (pathArray[1]) 时,它应该返回:“主机名:ScottTests”
  • 当深度为 3 个路径 (pathArray[2]) 时,它应该返回:“hostname: ScottTests: path3”
  • 当深度为 4 个路径 (pathArray[3]) 时,它应该返回:“hostname: ScottTests: path3: index.html”

任何帮助将不胜感激

提前致谢, 斯科特

【问题讨论】:

  • 您是否尝试将 URL 中的 '/' 替换为 ':'?
  • 您可以使用str = str.slice(0, -1); 来切断末尾的: 分隔符。请注意,只有在您知道最后会有 : 时才应该这样做。
  • 是的,所以我想将“/”替换为“:”(冒号和空格“)。例如 www.hostname.com/path1/path2/path3 将是 www.hostname.com : path1: path2: path3

标签: javascript arrays


【解决方案1】:

鉴于您对我关于 cmets 的问题的回答 - 我认为您需要做的是:

var slash = '/';
//we want to search for slash, globally
var regEx= new RegExp(slash, 'g');
//now replace the slash with ': ' as desired
var pageName = urlString.replace(regEx, ': ').trim();
//just in case we have : at the end
pageName = (urlString.endsWith(':')? pageName .slice(0, -1): pageName ); 

请试一试,如果有帮助,请告诉我们。 你可以看看 Javascript here.中的一些替换函数示例

【讨论】:

    【解决方案2】:

    我不完全确定我是否理解您的问题,但也许这会有所帮助。

    如果我在此 SO 页面的控制台中运行以下代码(即Using javascript to join the different parts of a url plus add a delimiter except on end of last array variable):

    var pathArray = window.location.toString().split('/');
    pathArray.slice(2, pathArray.length).join(': ');
    

    我得到以下输出:

    undefined
    "stackoverflow.com: questions: 39215590: using-javascript-to-join-the-different-parts-of-a-url-plus-add-a-delimiter-excep"
    


    此外,可以将元素推送到数组中:

    pathArray.push('additionalPath');
    pathArray.slice(2, pathArray.length).join(': ');
    

    输出:

    7
    "stackoverflow.com: questions: 39215590: using-javascript-to-join-the-different-parts-of-a-url-plus-add-a-delimiter-excep: additionalPath"
    


    或从中弹出:

    pathArray.pop();
    pathArray.pop();
    pathArray.slice(2, pathArray.length).join(': ');
    

    输出:

    "additionalPath"
    "using-javascript-to-join-the-different-parts-of-a-url-plus-add-a-delimiter-excep"
    "stackoverflow.com: questions: 39215590"
    


    这就是你要找的吗?

    【讨论】:

      猜你喜欢
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多