From ffa086975484ef3110525e5b5fd0f156d150a626 Mon Sep 17 00:00:00 2001 From: yizhi <946185759@qq.com> Date: Mon, 15 Nov 2021 21:36:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0&#xxxx;=E7=9A=84=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 +- src/index.ts | 6 +++--- test.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index cb3791f..d668584 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yizhi-html-escape", - "version": "1.0.1", + "version": "1.0.2", "description": "HTML escape character util", "main": "dist/index.js", "types": "typing/index.d.ts", diff --git a/src/index.ts b/src/index.ts index e1c0d9e..7ffed0c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -26,9 +26,9 @@ export function escape(str: string) { * @returns */ export function unescape(str: string) { - return str.replace(/&(\w+);/g, (match, name, index) => { - const code = unescapeTable[name] - if (!code) return match + return str.replace(/&((\w+)|(#\d+));/g, (match, name) => { + const code = (name[0] == '#') ? parseInt(name.substr(1)) : unescapeTable[name] + if (!code || isNaN(code)) return match return String.fromCharCode(code) }) } diff --git a/test.js b/test.js index c62cc1d..7750eb2 100644 --- a/test.js +++ b/test.js @@ -2,6 +2,6 @@ const {escape, unescape} = require('./dist/index') const str = `π=3.14, ¥¢£©®` const str1 = escape(str) -const str2 = unescape(str1) +const str2 = unescape(str1 + '你') console.log(str1) console.log(str2) \ No newline at end of file