I want to be able to extract a certain part of the classes only if it contains a certain value.
Let's say I have <div class="aa bb blog-xxx"></div>
I want to be able to only extract blog-xxx
{{Click Classes}} is a variable from Google Tag Manager
I have tried the following code:
function blogChecker(string) {
var el = {{Click Classes}};
if (el.includes("blog")) {
return el;
}
}
which extracts "aa bb blog-xxx" but I only want "blog-xxx"
Update I got it to work using the following code:
function() {
var el = {{Click Element}};
var regex = /blog.*/;
var classes = el.className.match(regex);
return classes;
}
indexOf, eg:el.indexOf('blog')