1

How might I figure out the category based on the CSS classes on the body please? Here is the body element:

<body class="archive category category-basketball category-23872 content-sidebar">

I want to output DFP page targeting for a category archive page. The format for the DFP page targeting code is

googletag.pubads().setTargeting( "category", "basketball" );

Therefore, I want to check for the existence of a category-{something} where {something} is the value for the category. Unfortunately, the 2nd category-23872 complicates the issue.

Appreciate any help and ideas. Thanks

2
  • look up regular expressions. Commented Apr 9, 2019 at 13:34
  • Do you want to check for all category-FOOBAR where FOOBAR != integer? As pointed out by @DanielA.White regex should be doing that magic for you ;) Commented Apr 9, 2019 at 13:35

1 Answer 1

1

You can use regular expressions and match().

let str =  `<body class="archive category category-basketball category-23872 content-sidebar">`;
let res = str.match(/category-[^\s0-9]+/g)
console.log(res)

Sign up to request clarification or add additional context in comments.

3 Comments

I think he meant except category-23872
@iLuvLogix Fixed that
sweet - that looks better now ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.