I'm trying to add the following logic to the below Custom Javascript variable within GTM: "Find if a string contains this word, and return that word"
function() {
var el = {{Click Element}};
el = el.parentElement;
var classes = el.className;
return classes ;
}
In the above code the classes variable returns the following string:
'wp-block-button jungle-cta tiger-animal'
I'm trying to do the following: if the string contains "tiger" then return "tiger-animal" and only that.
I am applying this function on different CTAs on the same landing page. Each CTA has a tiger constant as part of the class name. So one CTA is "tiger-animal", the other "tiger-something", "tiger-word" etc. I'm looking for logic that is agnostic of what follows after "tiger-". If the tiger is found then parse that word which contains the tiger and return it.
Update:
The below works now! Thank you.
function() {
var el = {{Click Element}};
el = el.parentElement;
var classes = el.className;
var regex = /tiger-[a-z]*/g;
var found = classes.match(regex);
return found[0]
}