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

Use regex to remove all special characters from string using thymeleaf

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

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

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

尝试使用这样的代码:

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

祝你好运!

Java Strings 已经有一个方法来替换 w/regex: string.replaceAll('...', '...')。在你的情况下,你可以简单地做:

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