admin管理员组文章数量:1221774
So I have an input[type="text"]
, where I want to paste JSON object as configuration. Output in console is perfect, all inline without any trimming, but now in that input
I have lots of spacing. I'd like to get rid of that spacing and replace the input
value.
Example
$('#widget-json').on('input propertychange', function () {
var string = this.value.replace(/\s+/g, ''),
data = $.parseJSON( string );
$(this).val( string );
});
That almost do the job, but it also removes spacing within quotes. So if I had a key/val like "sentence": "Sure thing, good to go."
, that would get converted into "sentence":"Surething,goodtogo."
, while I'd want to preserve spacing within quotes.
JSON Object Example
{
"widget-effect": 3,
"widget-opacity-color": "#C7C9CF",
"widget-opacity-slider": "50%",
"widget-opt-close": false,
"widget-opt-scroll": true,
"widget-opt-totop": true,
"widget-text": "Spacing required within quotes"
}
Desired Output Example
{"widget-effect":3,"widget-opacity-color":"#C7C9CF","widget-opacity-slider":"50%","widget-opt-close":false,"widget-opt-scroll":true,"widget-opt-totop":true,"widget-text": "Spacing required within quotes"}
- I tried,
jQuery.trim( this.value )
and that did not work at all. - I tried,
this.value.replace(/\s+/g, '')
and that removed entire whitespace, even within quotes. I know that it is probably correct outcome, but I have no ideas how to remove it only outside of quotes.
I am assuming that that regex could be fitted to skip replacing spacing inside of quotes, but I'm not really familiar with it at all.
So I have an input[type="text"]
, where I want to paste JSON object as configuration. Output in console is perfect, all inline without any trimming, but now in that input
I have lots of spacing. I'd like to get rid of that spacing and replace the input
value.
Example
$('#widget-json').on('input propertychange', function () {
var string = this.value.replace(/\s+/g, ''),
data = $.parseJSON( string );
$(this).val( string );
});
That almost do the job, but it also removes spacing within quotes. So if I had a key/val like "sentence": "Sure thing, good to go."
, that would get converted into "sentence":"Surething,goodtogo."
, while I'd want to preserve spacing within quotes.
JSON Object Example
{
"widget-effect": 3,
"widget-opacity-color": "#C7C9CF",
"widget-opacity-slider": "50%",
"widget-opt-close": false,
"widget-opt-scroll": true,
"widget-opt-totop": true,
"widget-text": "Spacing required within quotes"
}
Desired Output Example
{"widget-effect":3,"widget-opacity-color":"#C7C9CF","widget-opacity-slider":"50%","widget-opt-close":false,"widget-opt-scroll":true,"widget-opt-totop":true,"widget-text": "Spacing required within quotes"}
- I tried,
jQuery.trim( this.value )
and that did not work at all. - I tried,
this.value.replace(/\s+/g, '')
and that removed entire whitespace, even within quotes. I know that it is probably correct outcome, but I have no ideas how to remove it only outside of quotes.
I am assuming that that regex could be fitted to skip replacing spacing inside of quotes, but I'm not really familiar with it at all.
Share Improve this question asked Jul 19, 2015 at 14:06 dvldendvlden 2,4628 gold badges39 silver badges61 bronze badges 2- 5 Parse it and re-serialize it. – Pointy Commented Jul 19, 2015 at 14:06
- What was the actual desired behavior? – Zargold Commented Jul 19, 2015 at 14:22
2 Answers
Reset to default 14Use regex alternation operator.
var s = '"sentence": "Sure thing, good to go."';
alert(s.replace(/("[^"]*")|\s/g, "$1"))
What actually the above regex do?
("[^"]*")
captures all the double quoted blocks. So in the above,"sentence"
and"Sure thing ..."
are captured (means this particular part would be stored into a temp buffer of index 1).|
OR\s
matches all the space characters from the remaining string. So it won't touch the previous matched parts.$1
in the replacement part refers all the chars which are captured by the first capturing group. So chars in the first capturing groups are retained and the matched spaces got removed.
Update:
Unfortunately, when you escape a quote in the value of the string the regex breaks and stops removing spaces for the remaining key/value pairs
var stri = `"sentence \\"with escaped quotes\\" should not break": "Sure thing, good to go."`;
alert(stri.replace(/("(?:\\"|[^"])*")|\s/g, "$1"));
Try utilizing JSON.stringify
$("#widget-json").on("input propertychange", function () {
// var string = this.value.replace(/\s+/g, ''),
var data = JSON.stringify($.parseJSON( this.value ));
$(this).val( data );
});
var data = {
"widget-effect": 3,
"widget-opacity-color": "#C7C9CF",
"widget-opacity-slider": "50%",
"widget-opt-close": false,
"widget-opt-scroll": true,
"widget-opt-totop": true,
"widget-text": "Spacing required within quotes"
};
document.write(JSON.stringify(data));
本文标签: javascriptRemove white space from JSON objectbut not within quotesStack Overflow
版权声明:本文标题:javascript - Remove white space from JSON object, but not within quotes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739313488a2157693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论