2017  Kodetalk | Feedback | Privacy Policy | Terms | About

Regular expression to remove special characters in JSTL tags

I need to some way to integrate the regular expression functionality in JSTL.

e.g support the string contain AB then I want to display like A,B or A/B or A B etc.


I wiil suggest instead of using any JSTL function, you can manipulate the data before publishing to JSP page. I mean if you want to remove special character, then remove from the origin place.


Before My explanation I need to clear that, If you are thinking to use fn:replace().

The JSTL fn:replace() does not use a regular expression based replacement. It's just an exact charsequence-by-charsequence replacement, exactly like as String#replace() does.

Here is the complete evaluation for your requirement,

package com.example;

public final class Functions {

     private Functions() {



     public static String replaceAll(String string, String pattern, String replacement) {

         return string.replaceAll(pattern, replacement);



Which you can register in a /WEB-INF/functions.tld file as follows:

<?xml version="1.0" encoding="UTF-8" ?>




    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"


    <display-name>Custom Functions</display-name>    






        <function-signature>java.lang.String replaceAll(java.lang.String, java.lang.String, java.lang.String)</function-signature>



And finally use as follows:

<%@taglib uri="http://example.com/functions" prefix="f" %>


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

Answer is