简体   繁体   English

除非所述字符串的一部分被Node中的特定字符包围,否则如何从字符串中删除所有空格?

[英]How can I remove all whitespace from a string except when part of said string is surrounded by a specific character in Node?

Say we have this string:假设我们有这个字符串:

this is an example: 'this is too'

I need this output:我需要这个 output:

thisisanexample:'this is too'

I need a method to be able to remove whitespace in a string except where any given section is surrounded by a character, in this case, ' .我需要一种能够删除字符串中的空格的方法,除非任何给定的部分都被一个字符包围,在这种情况下是'

If it helps, I found this post here , but it's in python and I have just about no idea how to translate that.如果有帮助,我在这里找到了这篇文章,但它在 python 中,我几乎不知道如何翻译。

If you're wondering, this is for a data parser.如果您想知道,这是针对数据解析器的。

Basically just like the one you mentioned for python, this code split (using .split() ) the string and then iterate over the new array and check if the current element iterated is part of the string that should have the spaces removed (eg first element indexed [0]) is not part of the string inside '' and the check is done by % , and if its not it will add it again in '' and in the end it we .filter() the array and .join() its elements to get out a new string.基本上就像您提到的 python 一样,此代码拆分(使用.split() )字符串,然后迭代新数组并检查当前迭代的元素是否是应该删除空格的字符串的一部分(例如,首先索引为 [0]) 的元素不是''内字符串的一部分,检查由%完成,如果不是,它将再次将其添加到''中,最后我们.filter()数组和.join ()它的元素得到一个新的字符串。

so I think this is what you want:所以我认为这就是你想要的:

 function splitStr(str) { let strArr = str.split("'"); let newStr = ""; strArr.forEach((el, i) => { if(i % 2 === 0) { strArr[i] = el.replace(/\s/g, ''); } else { strArr[i] = `'${strArr[i]}'`; } }) return strArr.filter(String).join(''); } let str = "this is an example:'this is too'"; console.log(splitStr(str)); let str1 = "('test 2': 'the result' )"; console.log(splitStr(str1)); let str2 = "('test 2': the result )"; console.log(splitStr(str2));

You can accomplish this behavior using the trim() method, the ternary operator & with this sexy one-liner:您可以使用trim()方法、 三元运算符& 和这个性感的单行符来完成此行为:

const trimString = str => str[0] == "'" ? str : str.trim()

trimString('\'  John Snow  \'')
trimString('  John Summer ')

Check this working code sample .检查此工作代码示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从特定字符中删除部分字符串? - How do I remove part of a string from a specific character? 从字符串中删除除 ' 字符以外的所有标点符号 - Remove all punctuation from string except for ' character 给定一个字符串,如何使用 JavaScript 删除除特定“标签”(及其“子项”)之外的所有“HTML 标签”? - Given a string, how can I use JavaScript to remove all 'HTML tags' except a specific 'tag' (and its 'children')? 删除所有空格,除非空格是精确字符串的一部分 - Remove all whitespaces except when space is part of an exact string 除少数几个外,如何删除字符串中的所有HTML标记? - How can I remove all HTML tags in a string except for a few? 正则表达式-从第一个数字后的字符串中删除特定字符 - Regex - remove a specific character from a string except the first after number 如何从字符串以及所有字母中删除特定的特殊字符 - How to remove a specific special character from a string along with all Alphabets 查找字符字符,除非被特定字符包围 - Find character characters except when surrounded by specific characters 如何从字符串的特定部分对列表进行数字排序 - How can I sort a list numerically from a specific part of a string 如何从javascript中的字符串中删除每个第n个字符? - How can I remove every nth character from a string in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM