Home > database >  Write regex for testing a param is present in URL
Write regex for testing a param is present in URL

Time:01-17

I have the following reg ex and I am new to this: https:\/\/www.google-analytics.com\/.*

And the strings I am using to test are:

https://www.google-analytics.com/collect?v=1&ec=click&ea=test
https://www.google-analytics.com/collect?v=1&ea=tes

Currently, both strings will pass this check as I am doing /.* at the end of the regex, but what I want is this should check for ec=click param only and the ONLY the first test string should pass (https://www.google-analytics.com/collect?v=1&ec=click&ea=test)... does anyone know how I can do this?

Link to playground: https://regex101.com/r/mIjGjD/1

Thank you in advance

CodePudding user response:

https:\/\/www.google-analytics.com\/(.*)&ec=click(.*)

I guess this is what you want

CodePudding user response:

This regex should work. Notice that I add a map function in order to get all possible matches in an array of urls

const urls = [
"https://www.google-analytics.com/collect?v=1&ec=click&ea=test",
"https://www.google-analytics.com/collect?v=1&ea=test"
];

const reg = /https:\/\/www\.google-analytics\.com\/.*&ec=click.*/;

const matches = urls.map(url => url.match(reg) || []).flat();

console.log(matches)

  •  Tags:  
  • Related