使用 Thymeleaf 和正则表达式从字符串中移除所有特殊字符

4

我对thymeleaf还不太熟悉,最近我在试图从字符串中删除特殊字符。下面的代码有效,但需要逐个替换每个特殊字符。

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

是否有任何方法可以使用Thymeleaf中的单个正则表达式从字符串中删除所有特殊字符?


1
在你的例子中,哪些字符被称为“特殊字符”?(只有'''', '-'还是全部?) - user11116003
那数字呢? - user11116003
所有特殊字符以及数字。例如,我们可以使用“/ [^ A-Za-z0-9 \ -] /”表达式来删除所有特殊字符。 - buttercup
2个回答

5

Java String已经有一个使用正则表达式替换的方法:string.replaceAll('...', '...')。在您的情况下,您可以简单地使用以下方法:

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

1

尝试使用类似以下的代码:

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

祝你好运!


我正在尝试更改以下HTML片段:<div class="auctions-info-wrap" th:object="${auction}" th:with="auctionDetailLink=${wbParams != null AND wbParams['wb_auctiondetailsbaseurl'] != null ? wbParams['wb_auctiondetailsbaseurl'] : '/auction-detail/'} + |{#strings.toLowerCase(#strings.replace(#strings.replace(#strings.replace(name, '/','-'), '&',''),' ','-'))}/{guid}/|"> - buttercup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接