Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 512
Saurabh Arora Avatar asked Sep 24 '19 07:09

Saurabh Arora


People also ask

How does string split work in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

What is a separator in JavaScript?

separator: It is optional parameter. It defines the character or the regular expression to use for breaking the string. If not used, the same string is returned (single item array). limit: It is optional parameter.

Can you index strings in JavaScript?

Introduction. A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Each character in a JavaScript string can be accessed by an index number, and all strings have methods and properties available to them.


1 Answers

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('-'));
like image 130
Ramesh Avatar answered Oct 01 '22 12:10

Ramesh