0

i have written a jsp code with values coming from other jsp and i need to remove the special characters in the string.But iam not able to remove special characters. Please help

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
       <script>
            function change(chars){
                var dchars=document.getElementById("chars").value;
                dchars = dchars.replaceAll("!@#$%^&*()+=[]\\\';,/{}|\":<>?", '');
            document.getElementById("chars").innerHTML=dchars;
            }
        </script>
    </head>
   <%
String res=request.getParameter("tes");
       %>
       <body onload="change(chars)" ><script>
        change(res)
</script>
     <div id="chars"> <%=res%></div>
    </body>
</html>
4

3 に答える 3

0

innerHTML を使用する際の問題は、特定の文字が自動的に HTML エンティティに&変換されることです。&amp;

function cleanCharsText(){

    var el = document.getElementById("chars");
    var txt = el.innerText || el.textContent;
    el.innerHTML = txt.replace( /[!@#$%^&*()+=\\[\]\';,/{}\|\":<>\?]/gi, '');
}

ただし<span> text </span>、chars 要素内に次のものがある場合、テキストのみを抽出しているため、上記の関数を実行すると HTML スパン タグが削除されます。

于 2012-12-12T12:16:12.510 に答える
0

div 要素には「値」はありません。挿入された innerHtml を使用する必要があります。

document.getElementById('chars').innerHTML = dchars;
于 2012-12-12T11:21:45.047 に答える
0

これを試して....

 document.getElementById('chars').innerHTML = dchars; //div has no value..

特殊文字とは、文字ではないものを意味すると仮定すると、解決策は次のとおりです。

alert(dchars.replace(/[^a-zA-Z ]/g, ""));

また

alert(dchars.replace(/[^a-z0-9\s]/gi, '')); //will filter the string down to just alphanumeric values
于 2012-12-12T11:29:15.517 に答える