replace forward slash with colon | Community
Skip to main content
June 10, 2022
Solved

replace forward slash with colon

  • June 10, 2022
  • 1 reply
  • 1124 views

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" 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Jennifer_Dungan

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

1 reply

Jennifer_Dungan
Community Advisor and Adobe Champion
Jennifer_DunganCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
June 10, 2022

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

xcode2295Author
July 1, 2022

Thank you!! Very helpful 🙂