Search code examples
javascriptunicodearabicright-to-left

Concatenate numeric value to the right side of an Arabic String in JS


var data = "عينات";

var num = "123";

var res = data + num; // returns عينات123

I am trying to concatenate numbers to the right side of the arabic text, but its jumping to the left side, also tried reversing the string, but didn't help.

Please suggest a solution in Javascript.

Thanks in Advance!


Solution

  • You can make use of Directional_Formatting_Codes:

    • LRE U+202A LEFT-TO-RIGHT EMBEDDING Treat the following text as embedded left-to-right.
    • PDF U+202C POP DIRECTIONAL FORMATTING End the scope of the last LRE, RLE, RLO, or LRO.

    var data = "عينات";
    var num = "123";
    var res = "\u202A" + data + "\u202C" + num
    console.log(res);