How do I strip spaces from a Twig variable?
You can use "replace" - much like you would in PHP - and replace the space with no space

{{ anchor_id|replace({' ': ''})


More info: https://twig.symfony.com/doc/3.x/filters/replace.html