使用 thymeleaf 使用正则表达式从字符串中删除所有特殊字符

but*_*cup 4 regex special-characters thymeleaf

我是 thymeleaf 的新手,最近我部分弄清楚了如何从字符串中删除特殊字符。以下代码有效,但我必须替换每个特殊字符。

${#strings.toLowerCase(#strings.replace(#strings.replace(#strings.replace(name, '''','-'), '&',''),' ','-'))}
Run Code Online (Sandbox Code Playgroud)

有没有办法让我可以使用单个正则表达式来使用 thymeleaf 从字符串中删除所有特殊字符?

Met*_*ids 5

JavaString已经有一个方法来替换 w/regex: string.replaceAll('...', '...')。对于您的情况,您可以简单地执行以下操作:

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