简体   繁体   中英

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?

Java String s 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\-]', ''))}

Try use some code like this:

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

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM