Why does a Javascript string replace word order for languages ​​from right to left?

I am curious why the next placeholder replacement for the right to left language (these are random Arabic characters) causes the formatted string to cancel all words.

'{0} تكنولوجيا'.replace('{0}', 'هلهل') => "هلهل تكنولوجيا" 

This behavior has been observed in the latest Chrome, FF, and Safari browsers. It preserves the word order in Node.

+5
source share
1 answer

This is not true. replace does exactly what you asked for: Replaces the first three letters of this string with هلهل ; I am going to do this four, not three, so the original and replacement are the same length (making it easier to see what happens):

 var before = '{00} تكنولوجيا'; var rep = 'هلهل'; var after = before.replace('{00}', rep); console.log("before", before.split("").map(toCharCode).join(", ")); console.log("rep ", rep.split("").map(toCharCode).join(", ")); console.log("after ", after.split("").map(toCharCode).join(", ")); function toCharCode(c) { var hex = c.charCodeAt(0).toString(16); hex = "0000".substr(hex.length - 4) + hex; return "U-" + hex; } 

Output:

  before U-007b, U-0030, U-0030, U-007d, U-0020, U-062a, U-0643, U-0646, U-0648, U-0644, U-0648, U-062c, U -064a, U-0627
 rep U-0647, U-0644, U-0647, U-0644
 after U-0647, U-0644, U-0647, U-0644, U-0020, U-062a, U-0643, U-0646, U-0648, U-0644, U-0648, U-062c, U -064a, U-0627

Note that the replacement sequence (U-0647, U-0644, U-0647, U-0644) is now at the beginning of the line.

What you see is a way to display a string . Since adjacent spaces of RTL characters are displayed from right to left, and now you have one RTL interval, which is shown like this: the replacement is at the beginning (right), and the text continues on the left. You used to have an incorrect LTR and RTL value that was shown using LTR (shown from left to right), followed by RTL (shown from right to left).

+2
source

All Articles