It cannot be 6, because when you calculate the length of a string, it also contains spaces. So,
var d = "I am a 香港人"; d.length //returns 10 d.replace(/\s+/g, "").length //returns 7, excluding spaces
FYI: Your site must be correctly encoded.
I think I found what you need. "I 香港人", it contains a
twice. So Using @PSL answer I found a way.
var d = "I am a 香港人"; var uniqueList=d.replace(/\s+/g, '').split('').filter(function(item,i,allItems){ return i==allItems.indexOf(item); }).join(''); console.log(uniqueList.length);
As you comment, I assume that you propose the word "I 香 港 人" between each word. Now i have changed the code
var d = "I am a 香 港 人"; var uniqueList=d.split(' ').filter(function(item,i,allItems){ return i==allItems.indexOf(item); }); console.log(uniqueList.length);
Praveen
source share