Search code examples
extractgoogle-tag-manager

Extract from a page path or url excluding a specific format


I am currently working on a Google Tag Manager regex formula but what it needs to do is just show a specific word/s from a page path or page url.

For example: /page/course/tag-manager-lesson1/? /page/course/tag-manager-topic1/?

1st Output is "tag-manager" then 2nd Output is either "lesson1" or "topic1"

I found this question closely similar but somehow a little different on what is being extracted.

Thanks for the help!


Solution

  • JavaScript Variable

    function(){
      var allMatches = Array.from("/page/course/tag-manager- 
      lesson1/?".matchAll(/.*(tag-manager)-(\w+)\//g))
      var result = [allMatches[0][1], allMatches[0][2]]
      return result;
    }
    

    This should work. Or you have other more complicated page path than this.