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

3
  • Are you trying to replace '/' with ':' in the URL? Commented Aug 29, 2016 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. Commented Aug 29, 2016 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 Commented Aug 29, 2016 at 21:52

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.