增加&#xxxx;的支持
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "yizhi-html-escape",
|
"name": "yizhi-html-escape",
|
||||||
"version": "1.0.1",
|
"version": "1.0.2",
|
||||||
"description": "HTML escape character util",
|
"description": "HTML escape character util",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "typing/index.d.ts",
|
"types": "typing/index.d.ts",
|
||||||
|
@ -26,9 +26,9 @@ export function escape(str: string) {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
export function unescape(str: string) {
|
export function unescape(str: string) {
|
||||||
return str.replace(/&(\w+);/g, (match, name, index) => {
|
return str.replace(/&((\w+)|(#\d+));/g, (match, name) => {
|
||||||
const code = unescapeTable[name]
|
const code = (name[0] == '#') ? parseInt(name.substr(1)) : unescapeTable[name]
|
||||||
if (!code) return match
|
if (!code || isNaN(code)) return match
|
||||||
return String.fromCharCode(code)
|
return String.fromCharCode(code)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
2
test.js
2
test.js
@ -2,6 +2,6 @@ const {escape, unescape} = require('./dist/index')
|
|||||||
|
|
||||||
const str = `π=3.14, ¥¢£©®`
|
const str = `π=3.14, ¥¢£©®`
|
||||||
const str1 = escape(str)
|
const str1 = escape(str)
|
||||||
const str2 = unescape(str1)
|
const str2 = unescape(str1 + '你')
|
||||||
console.log(str1)
|
console.log(str1)
|
||||||
console.log(str2)
|
console.log(str2)
|
Reference in New Issue
Block a user