var text = "古" var con = document.getElementById('rs'); var is32Bit = function(c){ return c.codePointAt(0) > 0xFFFF; } var codePointLength = function(s){ // so slow if more then long length var rs = s.match(/[\s\S]/gu) return rs ? rs.length : 0; } var hasRegExpU = function(){ try{ var pattern = new RegExp(".", "U"); return true }catch(e){ return false // is not U flag } } con.textContent = text; con.textContent += "\n" con.textContent += String.fromCodePoint(134071) con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += text.length con.textContent += "\n" con.textContent += "/^.$/.test(text): " + /^.$/.test(text) con.textContent += "\n" con.textContent += "/^.$/.test(String.fromCodePoint(134071)): " + /^.$/.test(String.fromCodePoint(134071)) con.textContent += "\n" con.textContent += "/^.$/.test(String.fromCodePoint(134071)): " + /^.$/.test(String.fromCodePoint(134071)) con.textContent += "\n" con.textContent += "/^.$/u.test(String.fromCodePoint(134071)): " + /^.$/u.test(String.fromCodePoint(134071)) // u is unicode char regexp con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += "text.charAt(0): " + text.charAt(0)||'undefined' con.textContent += "\n" con.textContent += "text.charAt(1): " + text.charAt(1)||'undefined' con.textContent += "\n" con.textContent += "text.charAt(2): " + text.charAt(2)||'undefined' con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += "text.charCodeAt(0): " + text.charCodeAt(0)||'undefined' con.textContent += "\n" con.textContent += "text.charCodeAt(1): " + text.charCodeAt(1)||'undefined' con.textContent += "\n" con.textContent += "text.charCodeAt(2): " + text.charCodeAt(2)||'undefined' con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += "text.codePointAt(0): " + text.codePointAt(0)||'undefined' con.textContent += "\n" con.textContent += "text.codePointAt(1): " + text.codePointAt(1)||'undefined' con.textContent += "\n" con.textContent += "text.codePointAt(2): " + text.codePointAt(2)||'undefined' con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += "is32Bit('古'): " + is32Bit("古"); con.textContent += "\n" con.textContent += "is32Bit('a'): " + is32Bit("a"); con.textContent += "\n" con.textContent += "is32Bit(String.fromCodePoint(134071)): " + is32Bit(String.fromCodePoint(134071)); con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += "String.fromCodePoint(21476): " + String.fromCodePoint(21476) con.textContent += "\n" con.textContent += "String.fromCodePoint(134071): " + String.fromCodePoint(134071) con.textContent += "\n" + '#'.repeat(64) + "\n" con.textContent += 'codePointLength("abc"): ' + codePointLength("abc") // 3 con.textContent += "\n" con.textContent += 'codePointLength("古bc"): ' + codePointLength("古bc") // 3 con.textContent += "\n"
<pre id="rs"> </pre>