Created
June 7, 2017 16:29
-
-
Save qzm/9c7c780094c31bc37b00e5de37ebba22 to your computer and use it in GitHub Desktop.
js过滤特殊的字符串,例如:😊
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 过滤特殊的字符 | |
* @param {any} 需要被过滤掉字符串 | |
* @returns 过滤后的结果 | |
*/ | |
function deepFilter(src) { | |
let dst, prop; | |
let type = Object.prototype.toString.call(src); | |
if (type === '[object Array]') { | |
dst = []; | |
} else if (type === '[object Object]') { | |
dst = {}; | |
} else if (type === '[object String]') { | |
return src.replace(/([\ud800-\udbff][\u0000-\uffff])/g, ''); | |
} else { | |
return src; | |
} | |
for (prop in src) { | |
if (src.hasOwnProperty(prop)) { | |
dst[prop] = deepFilter(src[prop]); | |
} | |
} | |
return dst; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment