user2638062
user2638062

Reputation: 73

Concat RTL string with LTR string in javascript

I'm facing trouble in concatenating Arabic string with English string but their order is being messed!

I tried + operator and str1.concat(..) but nothing works for me.

var a = 'english'
var b = 'أ.ب-000082-13'
var c = '000004-ر خ-2014.xml'

//var myCoolString =a + '\\' + b + '\\' + c;

var myCoolString =a.concat("\\",b,"\\",c) 

document.getElementsByTagName('output')[0].innerHTML = myCoolString;

The result was like this: english\أ.ب-000082-13\000004-ر خ-2014.xml

Upvotes: 2

Views: 4748

Answers (1)

Paul S.
Paul S.

Reputation: 66404

The characters your are looking for are \u202A, \u202B and \u202C

function wrap_dir(dir, str) {
    if (dir === 'rtl') return '\u202B' + str + '\u202C';
    return '\u202A' + str + '\u202C';
}


wrap_dir('ltr', a) + wrap_dir('ltr', '\\') + wrap_dir('rtl', b) + wrap_dir('ltr', '\\') + wrap_dir('ltr', c);
// "‪english‬‪\‬‫أ.ب-000082-13‬‪\‬‪000004-ر خ-2014.xml‬"

Not sure why c wanted to be LTR, maybe because it ends .xml?

Upvotes: 14

Related Questions