1

I understand that for a long time the |replace filter has supported regex modifiers, but I can't seem to get the syntax right for multiline searches.

For example, here I want to extract the internal markup within a list that's been generated via Markdown, so I can do something with the internals:

{% set html = html|replace('/\\n/', '')|replace('/^(<ul>)(.*)(<\\/ul>)$/', '$2') %}

That works, but I should be able to use a modifier with the second replace so I don't have to use the first one, right?

But none of these work:

{% set html = html|replace('/^(<ul>)(.*)(<\\/ul>)$/m', '$2') %}

{% set html = html|replace('/^(<ul>)(.*)(<\\/ul>)$/\\m', '$2') %}

{% set html = html|replace('/^(<ul>)(.*)(<\\/ul>)$/(?m)', '$2') %}

What am I doing wrong?

1 Answer 1

1

I'm on my phone, so I'm not able to test, but it looks to me like your regex is explicitly expecting the string you're testing to start with <ul> and end with </ul>, and despite the multiline flag, any line breaks at the start or end of the string will prevent a match.

Try removing the ^ and $ from the start and end your pattern match to disable explicit start and end of string matching.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.