3

I am trying to track certain metrics in google analytics, but I only want to grab the params, remove sensitive information and send them off as a comma separated string. Such that:

this=me&that=you

would be fired off to ga as:

this,that

I have tried using Angular's native URL parser but I think I may need something more complex and I am not versed enough in REGEX to yank these out simply. Any help is greatly appreciated.

5
  • You can use .split('&') and .split('=') to get the key-value pairs and then the parameters themselves. Commented Aug 4, 2015 at 20:16
  • ^ to tag on to that, you can also use location.search to get the parameters at the end of a url Commented Aug 4, 2015 at 20:17
  • Had to change something. Take this with a grain of salt, but might give you some idea how to accomplish this with regex: \?((?<paramName>[\w-_\.%]+)=[\w-_\.%]+&?)+$ Commented Aug 4, 2015 at 20:27
  • So in your example, would I just assign my string to 'paramName' and pass that into your regex? Commented Aug 4, 2015 at 20:33
  • 1
    Actually, that won't work in Javascript. Check this out for a JS example: regex101.com/r/pN3uH3/1 Commented Aug 4, 2015 at 20:37

1 Answer 1

1

You can replace all = + values with commas, and then right-trim the final comma (if any) with regexps and replace in JS like this:

var str = "this=me&that=you";
var result = str.replace(/=[^&]+(?:&|$)/g, ',').replace(/,$/,'');
document.getElementById("r").innerHTML = result;
<div id="r"/>

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

1 Comment

Please let me know if you need more clarification on this.

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.