Small developers parse the contents of unicode encoded transfer Chinese display problems

Applet background data is returned when, html content is the result of unicode encoded can not be displayed directly inside full of similar &#xxxx;character, which requires a separate parsing encoded content, micro-letter applet is not going to resolve special symbols, we have to manually convert .

Directly on the code, you can take the test:

 

/**
 * 解析段落的unicode字符,聊天记录的内容中有很多是编码过的
 */
function decodeUnicode(str) {
  var ret = '';
  var splits = str.split(';');
  for (let i = 0; i < splits.length; i++) {
    ret += spliteDecode(splits[i]);
  }
  return ret;
}


/**
 * 解析单个unidecode字符
 */
function spliteDecode(value) {
  var target = value.match(/\\u\d+/g);
  if (target && target.length > 0) {
    target = target[0];
    var temp = value.replace(target, '{{@}}');
    target = target.replace('\\u', '');
    target = String.fromCharCode(parseInt(target));
    return temp.replace("{{@}}", target);
  } else {
    // value = value.replace( '\\u', '' );
    // return String.fromCharCode( parseInt( value, '10' ) )
    return value;
  }
}

//调用
decodeUnicode(valueFiled.replace(/&#/g, '\\u'));

Results are as follows:

Guess you like

Origin www.cnblogs.com/mengyilingjian/p/11696074.html