删除所有双引号前和双引号后的空格。

huangapple go评论76阅读模式
英文:

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?

{&quot;1&quot;:&quot;b.) some pasta salad&quot;,&quot;10&quot;:&quot;a.) Yes, some bread&quot;,&quot;11&quot;:&quot;a.) eggs and toast&quot; }

英文:

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 = &#39;{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }&#39;
var test_string_format = test_string.replace(/^[ &#39;&quot;]+|[ &#39;&quot;]+$|( ){2,}/g,&#39;$1&#39;)
    
console.log(test_string_format)

<!-- end snippet -->

How can I use regex to get the desired output?

{&quot;1&quot;:&quot;b.) some pasta salad&quot;,&quot;10&quot;:&quot;a.) Yes, some bread&quot;,&quot;11&quot;:&quot;a.) eggs and toast&quot; }

答案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(/ *&quot; */g, '&#39;&quot;&#39;)
英文:

Replace zero or more spaces, followed by a quote, followed by zero or more spaces, with just the quote:

replaceAll(/ *&quot; */g, &#39;&quot;&#39;)

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const test_string = &#39;{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }&#39;

const trimmed = test_string.replaceAll(/ *&quot; */g, &#39;&quot;&#39;);

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*&quot;\s*/g, '"');

console.log(test_string_format);
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const test_string = &#39;{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }&#39;;

const test_string_format = test_string.replace(/\s*&quot;\s*/g, &#39;&quot;&#39;);

console.log(test_string_format);

<!-- end snippet -->

  • \s: matches any white-space character
  • *: matches zero or more of the prev character(\s)
  • &quot;: matches the double quote character

答案4

得分: 2

const regex = /(\s*[&quot;]\s*)/gm;

var test_string =  = `{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }
`;

console.log(test_string.replace(regex, &#39;&quot;&#39;))
英文:
const regex = /(\s*[&quot;]\s*)/gm;

var test_string =  = `{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }
`;

console.log(test_string.replace(regex, &#39;&quot;&#39;))

答案5

得分: 0

console.log('{ "1": "b.) some pasta salad", "10": "a.) Yes, some bread", "11": "a.) eggs and toast" }'
.replace(/( *\&quot; +)|( +\ *)/ig,'\''))
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    console.log(&#39;{&quot; 1 &quot;: &quot;b.) some pasta salad&quot;, &quot;   10&quot;: &quot;   a.) Yes, some bread&quot;, &quot;11  &quot;: &quot;   a.) eggs and toast  &quot; }&#39;
    .replaceAll(/( *\&quot; +)|( +\ *)/ig,&#39;&quot;&#39;))

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月18日 15:40:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定