javascriptuglifyjsgulp-uglify

uglify Invalid hex-character pattern in string


Here is my code:

var xxx = '\u{1F393}'

When I use gulp-uglify, I get the following error message:

'Invalid hex-character pattern in string'


Solution

  • Unfortunately, it appears that your gulp-uglify doesn't support unicode point escapes. For this emoji, you could write it as its functionally identical surrogate pair \ud83c\udf93.

    console.log('\u{1F393}');
    console.log('\ud83c\udf93');

    Here is a simple script to help you convert out the emoji's into their surrogate pairs:

    const str = '🎓';
    const str2 = '\u{1F393}';
    
    function getEscaped(str) {
      var ret = '';
      for (var i=0; i<str.length; i++) {
        var code = str.charCodeAt(i).toString(16);
    
        ret += '\\u' + code;
      }
      
      return ret;
    }
    
    const result = getEscaped(str);
    console.log(result);
    console.log(eval(`"${result}"`));
    console.log(getEscaped(str2));