英文:
remove all blank space before double and after double quote marks
问题
I've used below formula to remove all blank space before " and after " but not effort.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var test_string = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }'
var test_string_format = test_string.replace(/^[ '"]+|[ '"]+$|( ){2,}/g,'$1')
console.log(test_string_format)
<!-- end snippet -->
How can I use regex to get the desired output?
{"1":"b.) some pasta salad","10":"a.) Yes, some bread","11":"a.) eggs and toast" }
英文:
I've used below formula to remove all blank space before " and after " but not effort.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var test_string = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }'
var test_string_format = test_string.replace(/^[ '"]+|[ '"]+$|( ){2,}/g,'$1')
console.log(test_string_format)
<!-- end snippet -->
How can I use regex to get the desired output?
{"1":"b.) some pasta salad","10":"a.) Yes, some bread","11":"a.) eggs and toast" }
答案1
得分: 3
以下是翻译好的部分:
"Assuming that the string content is always valid JSON, you can do it like this:
<!-- begin snippet:js console: true -->
<!-- language:lang-js -->
var str = ' {" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " } ' ;
const res=JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(str)).map(e=>e.map(e=>e.trim()))))
console.log(res)
<!-- end snippet -->
I know, this is kind of a "goofy" solution, but it works for the given sample string.
The snippet parses the JSON string, then converts the resulting object into an array of arrays. Each element of each sub-array is then .trim()
-med and at the end of the operation the object is reconstructed via .Object.fromEntries()
and turned back into a JSON string via JSON.stringify()
. "
英文:
Assuming that the string content is always valid JSON, you can do it like this:
<!-- begin snippet:js console: true -->
<!-- language:lang-js -->
var str = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }'
const res=JSON.stringify(Object.fromEntries(Object.entries(JSON.parse(str)).map(e=>e.map(e=>e.trim()))))
console.log(res)
<!-- end snippet -->
I know, this is kind of a "goofy" solution, but it works for the given sample string.
The snippet parses the JSON string, then converts the resulting object into an array of arrays. Each element of each sub-array is then .trim()
-med and at the end of the operation the object is reconstructed via .Object.fromEntries()
and turned back into a JSON string via JSON.stringify()
.
答案2
得分: 2
replaceAll(/ *" */g, ''"')
英文:
Replace zero or more spaces, followed by a quote, followed by zero or more spaces, with just the quote:
replaceAll(/ *" */g, '"')
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const test_string = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }'
const trimmed = test_string.replaceAll(/ *" */g, '"');
console.log(trimmed);
<!-- end snippet -->
答案3
得分: 2
const test_string = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }';
const test_string_format = test_string.replace(/\s*"\s*/g, '"');
console.log(test_string_format);
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const test_string = '{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }';
const test_string_format = test_string.replace(/\s*"\s*/g, '"');
console.log(test_string_format);
<!-- end snippet -->
\s
: matches any white-space character*
: matches zero or more of the prev character(\s
)"
: matches the double quote character
答案4
得分: 2
const regex = /(\s*["]\s*)/gm;
var test_string = = `{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }
`;
console.log(test_string.replace(regex, '"'))
英文:
const regex = /(\s*["]\s*)/gm;
var test_string = = `{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }
`;
console.log(test_string.replace(regex, '"'))
答案5
得分: 0
console.log('{ "1": "b.) some pasta salad", "10": "a.) Yes, some bread", "11": "a.) eggs and toast" }'
.replace(/( *\" +)|( +\ *)/ig,'\''))
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log('{" 1 ": "b.) some pasta salad", " 10": " a.) Yes, some bread", "11 ": " a.) eggs and toast " }'
.replaceAll(/( *\" +)|( +\ *)/ig,'"'))
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论