简体   繁体   中英

Escaping double quotes

I have this code snippet -

function updateUserSettings() {
    var cat;
    <logic:iterate id="category" name="studyData" property="categories">
        cat = dojo.byId("<%=category%>");
    </logic:iterate>
    ........
    ........
}

I want to escape double quote in <%=category%> value, but I can't use tag to do that.

Is there any other way I can achieve this?

Try to avoid Scriptlet and use JSTL or expression like ${category}

You don't need to escape double quote use single quote around it like

cat = dojo.byId('${category}');

because JavaScript supports both double and single quotes for string.


EDIT:

You can use JSTL fn:replace() Function to replace all occurrences of a string with another string.

For example:

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
...

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:replace(string1,'first', 'second')}"/>

<p>Final String : ${string2}</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM