Regular expression to remove special characters in JSTL tags

I am working on a Spring application, and on a JSPX page, I need to dynamically load some values ​​from the property page and set them as a drop-down list using the options tag. I need to use the same text for parameter values ​​and for display, but for parameter values ​​I need to remove all special characters.

For example, if the value is mother’s uncle, then I need

<option value="MaternalUncle">Maternal Uncle</option> 

What I get

 <option value="Maternal Uncle">Maternal Uncle</option> 

There are 2 applications that can use this page and which property file to load depends on the application. If I load values ​​for application 1, the values ​​will be displayed correctly, the last value in application 1 is “Other” and does not have special characters. For application 2, it does not trim spaces where the last value is “Maternal Uncle”. repOptions in code is an ArrayList with values ​​loaded from the properties file. Here is my code:

 <select name="person" id="person"> <option value="na">Select the relationship</option> <c:forEach items="${repOptions}" var="repOption"> <option value="${fn:replace(repOption, '[^A-Za-z]','')}">${repOption}</option> </c:forEach> </select> 

The first application removes spaces, as this is the fourth in list 9. For app2, this is the last value, and the regex does not work. If I put Maternal Uncle as the first property for application2, then this works fine, but the requirements should have the latter option.

  <option value="${fn:replace(repOption, ' ','')}"> 

works for spaces, but there may be values ​​like Brother / Sister, so I need to remove / as well, so I use regex.

+6
source share
1 answer

JSTL fn:replace() does not use regular expression replacement. This is just the exact replacement for charsequence-by-charsequence, just like String#replace() .

JSTL does not offer another EL function for this. You could simply execute the main EL function, which delegates regex based on String#replaceAll() .

eg.

 package com.example; public final class Functions { private Functions() { // } public static String replaceAll(String string, String pattern, String replacement) { return string.replaceAll(pattern, replacement); } } 

What do you register in the file /WEB-INF/functions.tld as follows:

 <?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <display-name>Custom Functions</display-name> <tlib-version>1.0</tlib-version> <uri>http://example.com/functions</uri> <function> <name>replaceAll</name> <function-class>com.example.Functions</function-class> <function-signature>java.lang.String replaceAll(java.lang.String, java.lang.String, java.lang.String)</function-signature> </function> </taglib> 

And finally use as below:

 <%@taglib uri="http://example.com/functions" prefix="f" %> ... ${f:replaceAll(repOption, '[^A-Za-z]', '')} 

Or, if you are already in Servlet 3.0 / EL 2.2 or later (Tomcat 7 or later), in which EL began to support method calls with arguments, simply call the String#replaceAll() method on the string instance.

 ${repOption.replaceAll('[^A-Za-z]', '')} 

See also:

+27
source

Source: https://habr.com/ru/post/914730/


All Articles