ROT13 //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 [rev. #1]

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 (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 (Votes: 23)

4.78