Call [] .reverse on a line

Why

[].reverse.call("string"); 

crash (error in firefox, etc. returns the original string in chrome), do they work when all other array methods are called?

 >>> [].splice.call("string",3) ["i", "n", "g"] >>> [].map.call("string",function (a) {return a +a;} ) ["ss", "tt", "rr", "ii", "nn", "gg"] 
+7
source share
3 answers

Because .reverse() modifies the array and the strings are immutable.


You could borrow Array.prototype.slice to convert to an array, then flip and join it.

 var s = "string"; var s2 = [].slice.call(s).reverse().join(''); 

Just remember that in older versions of IE you cannot manipulate a string like Array.

+8
source

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'); // → 'rab    oof' // Where did the `𝌆` symbol go? Whoops! 

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#1ma%C3%B1ana%20man%CC%83ana naiveReverse('mañana mañana'); // → 'anãnam anañam' // Wait, so now the tilde is applied to the `a` instead of the `n`? WAT. 

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); // → 'anañam anañam rab 𝌆 oof' 
+5
source

See @am, I do not answer why it does not work. However, if you want to know how to do this, first convert it to an array:

 "string".split('').reverse().join(''); // "gnirts" 
+4
source

All Articles