The following method (or similar) is commonly used to modify a string in JavaScript:
// Don't use this! var naiveReverse = function(string) { return string.split('').reverse().join(''); }
In fact, all the answers posted so far are a variation of this template. However, there are some problems with this solution. For example:
naiveReverse('foo 𝌆 bar');
If you are wondering why this is happening, read the internal JavaScripts encoding . (TL; DR: 𝌆
is an astral symbol, and JavaScript provides it as two separate blocks of code.)
But more:
// To see which symbols are being used here, check: // http://mothereff.in/js-escapes
A good line to check for reverse string implementations is as follows :
'foo 𝌆 bar mañana mañana'
Why? Since it contains an astral symbol ( 𝌆
) (which is represented by surrogate pairs in JavaScript ) and a combining sign ( ñ
the last mañana
consists of two characters: U + 006E LATIN SMALL LETTER N and U + 0303 COMBINING TILDE).
The order in which surrogate pairs appear cannot be canceled, otherwise the astral symbol will no longer appear in the "inverse line". That's why you saw those
labels in the output for the previous example.
Combined labels are always applied to the previous character, so you should consider (U + 006E LATIN SMALL LETTER N) as the union mark (U + 0303 COMBINING TILDE) as a whole. If you reverse their order, it will merge the union mark with another character in the string. Therefore, to output an example, there was ã
instead of ñ
.
Hopefully this explains why all the answers posted so far are incorrect .
To answer your initial question - how to [correctly] change a string in JavaScript, I wrote a small JavaScript library that can expand a string in Unicode. I have no problems that I just mentioned. The library is called Esrever ; its code is on GitHub and it works in almost any JavaScript environment. It comes with a shell / binary utility, so you can easily undo lines from your terminal if you want.
var input = 'foo 𝌆 bar mañana mañana'; esrever.reverse(input);