Search code examples
javascriptarabic

How does JavaScript split work on Arabic plus English number strings?


When I tried splitting:

"بحد-8635".split('-')

then JavaScript gives me this result:

[0] - بحد,
[1] - 8635

console.log("بحد-8635".split('-'))

And when I tried splitting:

"2132-سسس".split('-')

it gives me this different result:

[0] - 2132
[1] - سسس

console.log("2132-سسس".split('-'))

How is this happening? How can this be implemented correctly?


Solution

  • It depends on how you type the string (left to right / right to left). In the provided question, "2132-سسس" was typed from left to right and "8635-بحد" was typed from right to left.

    Check the below snippet.

    console.log("Typed left to right:");
    console.log("2132-سسس".split('-'));
    console.log("8635-بحد".split('-'));
    
    console.log("---------------");
    
    console.log("Typed right to left as Arabians follow:");
    console.log("سسس-2132".split('-'));
    console.log("بحد-8635".split('-'));