4

Currently this will take the text from the textarea and replace all the characters that are specified in the charMap:

<form>
<textarea name="text" id="text" style="width:300px; height:200px;"></textarea><br />
<input type="button" name="submit" id="submit" value="submit" />
</form>

var charMap = {
    "Å":'x',
    "å":'y',
    "b":'z',
    "c":'f'
};

$('#submit').click(function() {

    var str = $('#text').val();
    var str_array = str.split('');

    for( var i = 0, len = str_array.length; i < len; i++ ) {
        str_array[ i ] = charMap[ str_array[ i ] ] || str_array[ i ];
    }

    foo = str_array.join('');
    $('#text').val(foo);

});

The problem is that it does not recognise the special characters. So it will replace 'b' and 'c' but not 'Å' and 'å'.

Any ideas?

2
  • 2
    Do you mean these specific replacements, or swapping the letter with the letter that is n characters from the end of the alphabet (where n is the current letters position in the alphabet)?
    – Hamish
    Nov 19, 2010 at 23:59
  • @Hamish - I'd like those specific replacements Nov 20, 2010 at 0:52

3 Answers 3

2

Now that I'm understanding the requirements better, you can just create a map of characters to replacements like this:

Example: http://jsfiddle.net/gaG28/2/

var charMap = {
    a:'z',b:'v',c:'n',d:'s',e:'d',
    f:'k',g:'e',h:'y',i:'j',j:'r',
    k:'f',l:'m',m:'a',n:'c',o:'q',
    p:'t',q:'g',r:'i',s:'b',t:'p',
    u:'l',v:'u',w:'h',x:'o',y:'w',z:'x'
};
var str = "abcdefghijklmnopqrstuvwxyz";

var str_array = str.split('');

for( var i = 0, len = str_array.length; i < len; i++ ) {
    str_array[ i ] = charMap[ str_array[ i ] ] || str_array[ i ];
}
str = str_array.join('');

This will also leave any characters alone that are not found in the map.

10
  • Thats great. The only problem I'm having is that I want to replace some special characters such as 'å' with '&aring;'. When I try using these character is returns a nothing. Nov 20, 2010 at 1:36
  • Thanks a lot patrick but your revision didn't work for me, please see my edit in the original question. Nov 20, 2010 at 1:52
  • @Dave - It seems to work for me. Here's an updated example with the characters you referenced in you question. If Å:'&Aring;' doesn't work for you, try placing the key in quotes, like "Å":'&Aring;'. When placing the key in quotes, pretty much any character should be accepted as they key name.
    – user113716
    Nov 20, 2010 at 2:30
  • Nice one, the double quotes sorted it Nov 20, 2010 at 4:57
  • @patrick dw - ACtually that's no better. It works perfectly in jsfiddle but it will not work in an actual HTML file. It will pickup the normal characters but not the special characters even if I wrap them in double quotes? Nov 21, 2010 at 13:42
2

Since .replace() acts on a string, and returns a string, you can chain multiple replace calls together:

var text = $(this).val().replace(/a/g, "z").replace(/b/g, "y").replace(/c/g, "x");
2
  • I also thought of doing it this way but I would like to do it using just one regular expression Nov 20, 2010 at 0:00
  • @DaveKingsnorth hmm...ok. I'm trying to think if that's even possible using JS regular expressions.
    – Alex
    Nov 20, 2010 at 0:04
0

Your original code is correct, however the problem is due to the charset of the page, try the following:

  1. Make sure your page is saved as UTF-8
  2. Include the following in your header

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

I was head scratching with the same problem, this resolved my issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.