Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

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" 

2 Replies

Avatar

Community Advisor

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....