Nunjucks: Filter "replace" not working with regexp

Created on 4 Feb 2015  路  7Comments  路  Source: mozilla/nunjucks

According to the release v1.1.0 ( https://github.com/mozilla/nunjucks/releases/tag/v1.1.0 ), the filter replace should accept RegExps for the first argument.
I am trying this

... | replace( /(https?:\/\/[^ ]+)/ , "<a href=\"$1\">$1</a>" )

And it is triggering:
Template render error:
unexpected token: /

Am I being noob somewhere?

Most helpful comment

This needs to be mentioned on the docs!

All 7 comments

I am having the same problem

Looks like the syntax would be:

replace( r/(https?:\/\/[^ ]+)/ , "<a href=\"$1\">$1</a>" )

(note the r before the regexp)

hm...seems like by using the "r", it stop triggering the error.
But did not apply the replacement either!

Yes, you need to prepend the regex with r, like r/foo/. It probably wasn't matching the regexp which is why the replacement didn't occur. Please reopen/file a new issue if you still have problems!

This needs to be mentioned on the docs!

r/ This is a pit.

(This info is for others searching for REGEX among issues)

Hello, I know that's darn ol' thread.
Just wanna add little info - you should not be using quotation marks "" but use apostroph marks '' instead.
Otherwise you get some errors.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kud picture kud  路  3Comments

dgirgenti picture dgirgenti  路  3Comments

sudhirbeldar picture sudhirbeldar  路  3Comments

atian25 picture atian25  路  5Comments

sandiprb picture sandiprb  路  5Comments