【Avalon】escape
[\uD800-\uDBFF][\uDC00-\uDFFF]
var rsurrogate = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g var rnoalphanumeric = /([^\#-~| |!])/g var escape = function(str) { //将字符串经过 str 转义得到适合在页面中显示的内容, 例如替换 < 为 < return String(str). replace(/&/g, '&'). replace(rsurrogate, function(value) { console.log('============') console.log(value) var hi = value.charCodeAt(0) var low = value.charCodeAt(1) return '&#' + (((hi - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000) + ';' }). replace(rnoalphanumeric, function(value) { console.log('------------') console.log(value) return '&#' + value.charCodeAt(0) + ';' }). replace(/</g, '<'). replace(/>/g, '>') } str = 'abcdefg123456789[aa]0中过<div>©| !---</div>' console.log(str) console.log(escape(str)) // abcdefg123456789[aa]0中过<div>&copy;| !---</div> var r = /([^\#-~| |!])/g // 排除\#-~ 或 空格 或 ! 得到匹配中文 var r1 = /([^\#-~])/g // # -(to) ~ console.log(r.test('12 ji')) //var rr = /[\u4e00-\u9fa5]/g //console.log(rr.test('12聚宽'))