Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 547
buttercup Avatar asked Oct 25 '25 08:10

buttercup


1 Answers

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\-]', ''))}
like image 63
Metroids Avatar answered Oct 27 '25 01:10

Metroids



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!