0

I really need your help with this.

I’d like to be able to compare a string against an array and use it to replace special Unicode codes.

var unicode_dictionary = {
    "\00E9": "é",
    "\00E0": "à"
}


var old_str = "rapport couvrant une p\00E9riode de 6 mois (f\00E9vrier \00E0 juillet)"

if (\CODE match from the unicode_dictionary is found in the old_str) { then

    replace every single instance of the \CODE with the corresponding
character resulting in the new string:

    var new_str = "rapport couvrant une période de 6 mois (février à juillet)"

}

I am really lost with this as my DB outputs unicode characters in the \0000 format. How do I make a custom replace function like the above

2 Answers 2

1

By using Regx. in string replace you can achieve this. See the below code

var unicode_dictionary = {
    "\\00E9": "é",
    "\\00E0": "à"
}


var old_str = "rapport couvrant une p\00E9riode de 6 mois (f\00E9vrier \00E0 juillet)"

function convert(){
  for(var key in unicode_dictionary){    
      var regx=new RegExp(key,'g')
      old_str=old_str.replace(regx,unicode_dictionary[key]);
   }
  alert(old_str);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<button onclick='convert()'>Convert</button>

Sign up to request clarification or add additional context in comments.

Comments

0

You can use a Regex in combination with its function capability: search for the pattern \\[hex digits] and replace it with the actual Unicode character, in one pass, for any code. As long as these codes represent valid Unicode characters, the following works:

var old_str = "rapport couvrant une p\\00E9riode de 6 mois (f\\00E9vrier \\00E0 juillet)";
var new_str = old_str.replace(/\\([\da-f]{4})/gi, function (a,b)
  {
     return String.fromCharCode(parseInt(b, 16));
  });

Note that I doubled the backslashes in the source string of the snippet because that is per Javascript rules. The single backslashes in your source text do not need this.

This parses exactly 4 hexadecimal characters. If there may be less but no more than 4, you can use the regex \\([\da-f]{1,4}). It needs a maximum limit because there is no end marker in the source sequence. That means that without the maximum of 4, a string such as

the number \\00224\\0022

-- intended the number "4" -- will be translated as

the number Ȥ"

because the Unicode codepoint U+0224 represents a capital Z with hook.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.