ROT13 v1.0 //JavaScript Repository

Description

Encodes and decodes strings into the ROT13 format (rotation of the 26 characters of the alphabet by 13 positions).
Created: 2005.11.01

Code ( Download )

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/string/rot13 [v1.0]

String.prototype.rot13 = function(){
    return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
    });
};

Example ( See example )

<script type="text/javascript">
//<![CDATA[

var s = "Jonas Raoni 5.3.5";

document.write(
    "<h2>S = ", s, "</h2>",
    "S.rot13() = ", s.rot13(),
    "<br />S.rot13().rot13() = ", s.rot13().rot13()

);

//]]>
</script>

Help

String.rot13(void): String
Encodes/decodes a string into the rot13 format.

Rank (5 votes)

5.00
0 1 2 3 4 5