从UTF-32字符中清除JS中的字符串

Cleaning string in JS from UTF-32 characters

我需要从 JS 中的字符串中清除以 UTF-32 编码的字符,例如“”。 我尝试使用代码:

str.replace(/[^\u0000-\uFFFF]/gi, '')

但这是行不通的。

对于干净的消息,我使用了

function fixedCharCodeAt(str, idx) {
  var code = str.charCodeAt(idx);
  if (0xD800 <= code && code <= 0xDBFF) { 
    // Upper auxiliary char
    var hi = code;
    var low = str.charCodeAt(idx+1);
    return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
 }
 if (0xDC00 <= code && code <= 0xDFFF) { 
   // Lower auxiliary symbol
    var hi = str.charCodeAt(idx-1);
    var low = code;
    return ((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
  }
  return code;
}

function cleaningMsgFromBreakingSymb(message_old) {
  var new_message = "";
  for (var i = 0, len = message_old.length; i < len; i++) {
    if (fixedCharCodeAt(message_old, i) < 65535){
        new_message += message_old[i];
    };
  };
  return new_message;
}