简体   繁体   English

javascript将所有出现的“,\\ S”替换为“,\\ S”

[英]javascript replace all occurrences “,\S” with “, \S”

I want to have spaces separating items in a CSV string. 我想用空格分隔CSV字符串中的项目。 That is "123,456,789" => "123, 456, 789". 那是“ 123,456,789” =>“ 123,456,789”。 I have tried, but been unable to construct a regexp to do this. 我已经尝试过,但是无法构造一个正则表达式来做到这一点。 I read some postings and thought this would to the trick, but no dice. 我读了一些帖子,以为这可以达到目的,但是没有骰子。

 text = text.replace(new RegExp(",\S", "g"), ", ");

Could anyone show me what I am doing wrong? 谁能告诉我我在做什么错?

You have two problems: 您有两个问题:

  1. Backslashes are a pain in the, um, backslash; 反斜杠是反斜杠中的一种痛苦。 because they have so many meanings (eg to let you put a quote-mark inside a string), you often end up needing to escape the backslash with another backslash, so you need ",\\\\S" instead of just ",\\S" . 因为它们有很多含义(例如,让您在字符串中加上引号),所以您常常最终需要用另一个反斜杠来转义反斜杠,因此您需要使用",\\\\S"而不是仅使用",\\S"
  2. The \\S matches a character other than whitespace , so that character gets removed and replaced along with the comma. \\S匹配空格以外的其他字符 ,因此该字符将被删除并与逗号一起替换。 The easiest way to deal with that is to "capture" it (by putting it in parentheses), and put it back in again in the replacement (with $1 ). 处理该问题的最简单方法是“捕获”它(通过将其放在括号中),然后再次放入替换中(使用$1 )。

So what you end up with is this: 因此,最终结果是:

text = text.replace(new RegExp(',(\\S)', "g"), ", $1");

However, there is a slightly neater way of writing this, because JavaScript lets you write a regex without having a string, by putting it between slashes. 但是,有一种稍微更整齐的编写方式,因为JavaScript允许您通过将其放在正斜杠之间而无需字符串来编写正则表达式。 Conveniently, this doesn't need the backslash to be escaped, so this much shorter version works just as well: 方便地,这不需要转义反斜杠,因此这个简短得多的版本同样适用:

text = text.replace(/,(\S)/g, ", $1");

As an alternative to capturing, you can use a "zero-width lookahead", which in this situation basically means "this bit has to be in the string, but don't count it as part of the match I'm replacing". 作为捕获的一种替代方法,您可以使用“零宽度前瞻”,在这种情况下,这基本上意味着“该位必须在字符串中,但不要将其视为我要替换的匹配项的一部分”。 To do that, you use (?=something) ; 为此,您可以使用(?=something) ; in this case, it's the \\S that you want to "look ahead to", so it would be (?=\\S) , giving us this version: 在这种情况下,是您要“向前看”的\\S ,因此它将是(?=\\S) ,为我们提供此版本:

text = text.replace(/,(?=\S)/g, ", ");

There are 2 mistakes in your code: 您的代码中有2个错误:

  1. \\S in a string literal translates to just S , because \\S is not a valid escape sequence. 字符串文字中的\\S仅转换为S ,因为\\S不是有效的转义序列。 As such, your regex becomes /,S/g , which doesn't match anything in your example. 这样,您的正则表达式将变为/,S/g ,与您的示例中的任何内容都不匹配。 You can escape the backslash ( ",\\\\S" ) or use a regex literal ( /,\\S/g ). 您可以转义反斜杠( ",\\\\S" )或使用正则表达式文字( /,\\S/g )。

  2. After this correction, you will replace the character following the comma with a space. 更正之后,您将用逗号替换逗号后面的字符。 For instance, 123,456,789 becomes 123, 56, 89 . 例如, 123,456,789变为123, 56, 89 There are two ways to fix this: 有两种方法可以解决此问题:

    1. Capture the non-space character and use it in the replacement expression: 捕获非空格字符并将其用于替换表达式中:

       text = text.replace(/,(\\S)/g, ', $1') 
    2. Use a negative lookahead assertion (note: this also matches a comma at the end of the string): 使用否定的超前断言(注意:这也匹配字符串末尾的逗号):

       text = text.replace(/,(?!\\s)/g, ', ') 
text = text.replace(/,(\S)/g, ', $1');

try this: 尝试这个:

var x = "123,456,789";
x = x.replace(new RegExp (",", "gi"), ", ");

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM