Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there js function that replace xml Special Character with their escape sequence?

I search the web alot and didn't find js function that replace xml Special Character with their escape sequence?
Is there something like this?

I know about the following:

Special Character   Escape Sequence Purpose  
&                   &           Ampersand sign 
'                   '          Single quote 
"                   "          Double quote
>                   >            Greater than 
<                   &lt;            Less than

is there more? what about writing hexadecimal value like 0×00,
Is this also a problem?

like image 495
Dor Cohen Avatar asked Mar 27 '12 14:03

Dor Cohen


People also ask

How do I change the escape character in XML?

Special characters (such as <, >, &, ", and ' ) can be replaced in XML documents with their html entities using the DocumentKeywordReplace service. However, since html entities used within BPML are converted to the appropriate character, the string mode of DocumentKeywordReplace will not work in this instance.

How do you escape a special character in JavaScript?

To use a special character as a regular one, prepend it with a backslash: \. . That's also called “escaping a character”.


1 Answers

I have used this:

function htmlSpecialChars(unsafe) {
    return unsafe
    .replace(/&/g, "&amp;")
    .replace(/</g, "&lt;")
    .replace(/>/g, "&gt;")
    .replace(/"/g, "&quot;")
    .replace(/'/g, "&apos;");
}
like image 117
nathanjosiah Avatar answered Nov 07 '22 18:11

nathanjosiah