As the title states I am attempting to display Unicode accent marks next to letters.
This task comes from needing to iterate through a string, identify a special character and then "simplify" it by breaking the accent mark and letter and displaying them side by side(the word being correct doesnt matter, only formatting matters).
i.e. Às --> Aˋs
I already have the unicode needed, so I do not need to identify any of the characters.
I'm attempting to do this dynamically, so I've stored all of the special character unicode and replacement unicode in objects within an array. Rather than iterate through every single character within the string, I'm globally replacing every instance of the special character with the new combination of unicode characters i want. please see my current code below:
//String to check for special characters
var string_data = "Às simple as this sounds...it is trivial"
//Array of special(incompatible) characters and replacement unicode characters
var unicodeChars = [
{
incompatible_unicode_char: "\u00C0",//À
replace_uni_char_one: "\u0041", //A
replace_uni_char_two: "\u0300" //ˋ
}
];
//Convert property values from unicodeChars objects to readable characters
for(var i = 0; i< unicodeChars.length;i++){
String.fromCharCode(parseInt(unicodeChars[i].incompatible_unicode_char,16));
String.fromCharCode(parseInt(unicodeChars[i].replace_uni_char_one,16));
String.fromCharCode(parseInt(unicodeChars[i].replace_uni_char_two,16));
}
//Iterate through each object in unicodeChars array
for(var i = 0; i<unicodeChars.length;i++){
//Creating a string that holds the value of what to replace the special character with
var replacement_chars = unicodeChars[i].replace_uni_char_one;
if(unicodeChars[i].replace_uni_char_two != null){
replacement_chars = replacement_chars + unicodeChars[i].replace_uni_char_two;
}
//creating regex object in order to globally replace any occurrence of the special character in the string
var regex = new RegExp(unicodeChars[i].incompatible_unicode_char, "g");
//attempting to replace the occurrence
string_data = string_data.replace(regex, replacement_chars);
}
My desired end value of string_data is: Aˋs simple as this sounds...it is trivial
However the problem here is that the current end value is: Às simple as this sounds...it is trivial
So string_data
is basically not changing at all, but at the same time it is. When investigating, I've found that adding characters and accent markers combines them into a single letter.
So in my code when I do the following: replacement_chars = replacement_chars + unicodeChars[i].replace_uni_char_two;
the code automatically combines the accent mark from the unicodeChars[i].replace_uni_char_two
with the standard letter held in replacement_chars
.
I do not want this combining to take place, I wish to display them next to each other like Aˋs
rather than Às
. How do I stop javascript from automatically combining the accent mark and standard letter?
Please keep in mind that I need to keep the current structure of this code in place (the array of unicodeCharacters, converting unicode values to characters, and then using regex to perform a global replace
) ahead of time and I wish to keep this solution dynamic as it currently is.