Expand my Community achievements bar.

SOLVED

replace forward slash with colon

Avatar

Level 4

Hi,

I am capturing the url as pageName and reading the pageName from the url

 

.Current url is https://www.xyz.com/abc/def/

 

Expected pageName = xyz:abc:def

 

What data element custom code should I write to get to the expected pageName? I want to replace forward slashes with colon. I am already using "window. location.href" 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor and Adobe Champion

I would use multiple "replace" functions:

 

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;

 

Other people may debate about using Regex Replace, but the it's still better than looping through the entire string and replacing each instance.... 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor and Adobe Champion

I would use multiple "replace" functions:

 

var url = window.location.href;

// removes the https://www. and .com in the url 
url = url.replace("https://www.", "").replace(".com","");

// replaces all / with :, and removes the last instance of : at the end of the string
var pageName = url.replace(/\//g, ":").replace(/:$/g, ""); 

return pageName;

 

Other people may debate about using Regex Replace, but the it's still better than looping through the entire string and replacing each instance....