Nonesome
Nonesome

Reputation: 98

How to use regular expressions (\b, \w) in Freemarker?

${str?replace("\d+", "", "r")};

I wanted to use \d to remove numbers, but it didn't work!!!

But ${str?replace("[0-9]", "", "r")}; works!!!

So, I wanna know how to use regex like \d, \b, \w, etc?

Upvotes: 6

Views: 12027

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

You need to double the backslashes:

${str?replace("\\d+", "", "r")};

This is because string escaping rules are applied before regex escaping rules. So the string "\\d" is translated to the regex \d which then matches a digit.

If your string is "\d", the string processor translates it to a literal d (because \d is not a recognized string escape sequence, so it's ignored).

Upvotes: 13

Related Questions