Skip to content

Instantly share code, notes, and snippets.

@qzm
Created June 7, 2017 16:29
Show Gist options
  • Save qzm/9c7c780094c31bc37b00e5de37ebba22 to your computer and use it in GitHub Desktop.
Save qzm/9c7c780094c31bc37b00e5de37ebba22 to your computer and use it in GitHub Desktop.
js过滤特殊的字符串,例如:😊
/**
* 过滤特殊的字符
* @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