buttercup
buttercup

Reputation: 125

Use regex to remove all special characters from string using thymeleaf

I am new to thymeleaf and recently I partially figured it out how to remove special characters from a string. Following code is working but I have to replace every single special character.

${#strings.toLowerCase(#strings.replace(#strings.replace(#strings.replace(name, '''','-'), '&',''),' ','-'))}

Is there any way around so that I can use a single regex expression to remove all special characters from a string using thymeleaf?

Upvotes: 4

Views: 3444

Answers (2)

Metroids
Metroids

Reputation: 20487

Java Strings already have a method to replace w/regex: string.replaceAll('...', '...'). In your case, You can simply do:

${#strings.toLowerCase(name.replaceAll('[^A-Za-z0-9\-]', ''))}

Upvotes: 5

user11116003
user11116003

Reputation:

Try use some code like this:

Regex regex1 = new Regex(@"[^A-Za-z0-9]");
strings.replace(name, "", regex1.match(name));

Good Luck!

Upvotes: 1

Related Questions