Parsing parses from url in Angular

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

this=me&that=you

      

will be passed to ga as:

this,that

      

I've tried using Angular's native parser, but I think I might need something more complex and don't know enough about REGEX to just pluck them. Any help is appreciated.

+3


source to share


1 answer


You can replace all =

+ values โ€‹โ€‹with commas and then right-trim the last comma (if any) with regex 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"/>
      

Run codeHide result


+1


source







All Articles