0

trying to join the different parts of a url plus add a delimiter for ": " (colon space) delimiter that does not get added to the last array value which could change. This is being used for a web analytics data layer.

I tried using the below however, it doesn't work if I have an array that is less than 4 and will add the delimiter to the end of the last variable.

Input:

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.

1st attempt - tried the below but it removes the last path (pathArray[3])

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

2nd attempt - still adds the delimiter to the end if path is less than 4

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

3rd attempt - still adds the delimiter to the end

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

Desired Output:

  • When 1 path deep (hostName) it should return: "hostname"
  • When 2 paths deep (pathArray[1]) it should return: "hostname: ScottTests"
  • When 3 paths deep (pathArray[2]) it should return: "hostname: ScottTests: path3"
  • When 4 paths deep (pathArray[3]) it should return: "hostname: ScottTests: path3: index.html"

any help will be greatly appreciated

thanks in advance, Scott

Daniel
  • 10,641
  • 12
  • 47
  • 85
  • Are you trying to replace '/' with ':' in the URL? – ishmaelMakitla Aug 29 '16 at 21:44
  • You can use `str = str.slice(0, -1);` To cut off the `:` delimiter at the end. Notice you should only do this when you know you will have an `:` at the end. – Aaron Aug 29 '16 at 21:47
  • yes, so I want to replace "/" with ": " (colon and a space"). ex. www.hostname.com/path1/path2/path3 would be www.hostname.com: path1: path2: path3 – Scott Tavares Aug 29 '16 at 21:52

2 Answers2

1

In view of what you responded to my question on comments - I think what you need to do is the following:

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 ); 

Please give this a try and let us know if this helps. You can have a look at some examples of replace function in Javascript here.

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
0

I'm not entirely sure I understand your question, but perhaps this will help.

If I run the following code in the console on this SO page (i.e. 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(': ');

I get the following output:

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


Additionally, elements can be pushed onto the array:

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

Output:

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


Or popped from it:

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

Output:

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


Is that what you're looking for?

Community
  • 1
  • 1
MJH
  • 2,301
  • 7
  • 18
  • 20