JS+JSON字符转义(escape)的几种方案

一、JSON.js

首先取自DouglasCrockford的方案,应该较多人使用。json.js和json2.js都差不多的。

// https://raw.github.com/douglascrockford/JSON-js/master/json.js
var meta = { // table of character substitutions
	'\b' : '\\b',
	'\t' : '\\t',
	'\n' : '\\n',
	'\f' : '\\f',
	'\r' : '\\r',
	'"' : '\\"',
	'\\' : '\\\\'
};
function quote(string) {
	// If the string contains no control characters, no quote characters, and no
	// backslash characters, then we can safely slap some quotes around it.
	// Otherwise we must also replace the offending characters with safe escape
	// sequences.

	escapable.lastIndex = 0;
	return escapable.test(string) ? '"'
			+ string.replace(escapable, function(a) {
						var c = meta[a];
						return typeof c === 'string' ? c : '\\u'
								+ ('0000' + a.charCodeAt(0).toString(16))
										.slice(-4);
					}) + '"' : '"' + string + '"';
}

二、依然取自D.C大大,在json_parse.js里。

// https://raw.github.com/douglascrockford/JSON-js/master/json_parse.js
var escapee = {
	'"' : '"',
	'\\' : '\\',
	'/' : '/',
	b : '\b',
	f : '\f',
	n : '\n',
	r : '\r',
	t : '\t'
};
string = function() {
	// Parse a string value.
	var hex, i, string = ', uffff;

	// When parsing for string values, we must look for " and \ characters.
	if (ch === '"') {
		while (next()) {
			if (ch === '"') {
				next();
				return string;
			} else if (ch === '\\') {
				next();
				if (ch === 'u') {
					uffff = 0;
					for (i = 0; i < 4; i += 1) {
						hex = parseInt(next(), 16);
						if (!isFinite(hex)) {
							break;
						}
						uffff = uffff * 16 + hex;
					}
					string += String.fromCharCode(uffff);
				} else if (typeof escapee[ch] === 'string') {
					string += escapee[ch];
				} else {
					break;
				}
			} else {
				string += ch;
			}
		}
	}
	error("Bad string");
}

三、仍为D.C大大的,在json_parse_state.js里。

// https://raw.github.com/douglascrockford/JSON-js/master/json_parse_state.js
var escapes = { // Escapement translation table
	'\\' : '\\',
	'"' : '"',
	'/' : '/',
	't' : '\t',
	'n' : '\n',
	'r' : '\r',
	'f' : '\f',
	'b' : '\b'
};
function debackslashify(text) {
	// Remove and replace any backslash escapement.
	return text.replace(/\\(?:u(.{4})|([^u]))/g, function(a, b, c) {
				return b ? String.fromCharCode(parseInt(b, 16)) : escapes[c];
			});
}

四、旧版的json.js,moontools也是这种方案的?。这里/[\x00-\x1f\\"]/g代表什么意思呢?

// JSON用特殊字符转义
var special = {
	'\b' : '\\b',
	'\t' : '\\t',
	'\n' : '\\n',
	'\f' : '\\f',
	'\r' : '\\r',
	'"' : '\\"',
	'\\' : '\\\\'
};
var escape = function(chr) {
	return special[chr] || '\\u'
			+ ('0000' + chr.charCodeAt(0).toString(16)).slice(-4)
};

function _escape(obj){
	return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"'
}

五、我当前的方案,感觉比较简单明了,但会不会有未知的问题呢?待继续明了。

function StringAs(string) {
	return '"' + string.replace(/(\\|\"|\n|\r|\t)/g, "\\$1") + '"';
}

六、最后发现这里也有http://relucent.iteye.com/blog/646016


源地址:

http://blog.csdn.net/zhangxin09/article/details/6582173


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值