JavaScript str_rot13
Perform the rot13 transform on a string
1 2 3 4 56 7 8 9 1011 12 13 14 1516 | function str_rot13 (str) { // Perform the rot13 transform on a string // // version: 909.322 // discuss at: http://phpjs.org/functions/str_rot13 // + original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com) // + improved by: Ates Goral (http://magnetiq.com) // + bugfixed by: Onno Marsman // * example 1: str_rot13('Kevin van Zonneveld'); // * returns 1: 'Xriva ina Mbaariryq' // * example 2: str_rot13('Xriva ina Mbaariryq'); // * returns 2: 'Kevin van Zonneveld' return (str+'').replace(/[A-Za-z]/g, function (c) { return String.fromCharCode((((c = c.charCodeAt(0)) & 223) - 52) % 26 + (c & 32) + 65); });} |
Examples
» Example 1
Running
1 | str_rot13('Kevin van Zonneveld'); |
Should return
1 | 'Xriva ina Mbaariryq' |
» Example 2
Running
1 | str_rot13('Xriva ina Mbaariryq'); |
Should return
1 | 'Kevin van Zonneveld' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have str_rot13 goodness in JavaScript.
Not sure why it didn't make it, though at least str_rot13 with or without JavaScript made it to the first page. Thanks to your prompting, I went ahead and did a little further SEO investigation of the site and sent the recommendations to Kevin (though he's already done a fine job of bringing his site to sometimes even rival the PHP site when you search for PHP functions)
Great!, only like 3 lines of code, I like this alot and its just what I'm looking for. Why on earth is this page not listed on Google for #1 spot of "rot13 javascript"?, on Google all I could find where old rot13 implementations that has like 40 lines of code written years ago using schemes for javascript 1.0. Glad I found this website! Thanks alot.
Alternative implementation that doesn't use conditionals:
1
2
3
4
56
7
8
| function str_rot13(str) { // * example 1: str_rot13("Hello World!"); // * returns 1: "Uryyb Jbeyq!" // * example 2: str_rot13(str_rot13("Hello World!")); // * returns 2: "Hello World!" return str.replace(/[A-Za-z]/g, function (c) { return String.fromCharCode((((c = c.charCodeAt(0)) & 223) - 52) % 26 + (c & 32) + 65); }); } |


Kevin van Zonneveld
4 Aug '09