Javascript 正则表达式用值替换所有逗号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13955904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:15:41  来源:igfitidea点击:

Regex replace all commas with value

javascriptregex

提问by Jeff

I have a string that looks like this: "Doe, John, A" (lastname, firstname, middle initial).

我有一个看起来像这样的字符串:“Doe,John,A”(姓氏,名字,中间名首字母)。

I'm trying to write a regular expression that converts the string into "Doe*John*A".

我正在尝试编写一个将字符串转换为“Doe*John*A”的正则表达式。

However, I have to take into account all spaces for this string so "Doe , John , A" would still convert into "Doe*John*A".

但是,我必须考虑此字符串的所有空格,因此“Doe,John,A”仍会转换为“Doe*John*A”。

ALSO, the string "Doe John A" should convert into "Doe*John*A".

此外,字符串“Doe John A”应该转换为“Doe*John*A”。

I started writing this, but I think I'm stuck on the spaces & the possibility of the user not supplying the commas.

我开始写这个,但我想我被困在空格和用户不提供逗号的可能性上。

Here's what I have:

这是我所拥有的:

var myString = "John, Doe, A";
var myOtherString = "John  Doe   A";


var myFunction = function (aString) {
        aString = aString.replace(", ", "*");
        aString = aString.replace(", ", "*");

return aString;

};

These should both return "Doe*John*A".

这些都应该返回"Doe*John*A"

I think I'm repeating myself too much in this function. I'm also not taking into account the possibility that no commas will be provided.

我想我在这个功能中重复了太多。我也没有考虑不提供逗号的可能性。

Is there a better way to do this?

有一个更好的方法吗?

回答by Martin Ender

Yes, there is. Use the replacefunction with a regex instead. That has a few advantages. Firstly, you don't have to call it twice anymore. Secondly it's really easy to account for an arbitrary amount of spaces and an optional comma:

就在这里。改用replace带有正则表达式的函数。这有几个优点。首先,你不必再调用它两次了。其次,考虑任意数量的空格和可选的逗号真的很容易:

aString = aString.replace(/[ ]*,[ ]*|[ ]+/g, '*');

Note that the square brackets around the spaces are optional, but I find they make the space characters more easily readable. If you want to allow/remove any kind of whitespace there (tabs and line breaks, too), use \sinstead:

请注意,空格周围的方括号是可选的,但我发现它们使空格字符更易于阅读。如果您想允许/删除任何类型的空格(也包括制表符和换行符),请\s改用:

aString = aString.replace(/\s*,\s*|\s+,/g, '*');

Note that in both cases we cannot simply make the comma optional, because that would allow zero-width matches, which would introduce a *at every single position in the string. (Thanks to CruorVult for pointing this out)

请注意,在这两种情况下,我们不能简单地将逗号设为可选,因为这将允许零宽度匹配,这将*在字符串中的每个位置引入 a 。(感谢 CruorVult 指出这一点)

回答by CruorVult

If you want to replace all non-word characters try this:

如果要替换所有非单词字符,请尝试以下操作:

str.replace(/\W+/g, '*');

回答by Philipp

String.replace only replaces the first occurence. To replace them all, add the "g" flag for "global". You can also use character groups and the +operator (one or more) to match chains of characters:

String.replace 只替换第一次出现。要全部替换它们,请为“global”添加“g”标志。您还可以使用字符组和 + 运算符(一个或多个)来匹配字符链:

aString.replace("[,\s]+", "*", "g");

This will replace all chains of commas and whitespaces with a *.

这会将所有逗号和空格链替换为*.

回答by bonCodigo

Try this out to remove all spaces and commas, then replace with *.

试试这个删除所有空格和逗号,然后用 * 替换。

Myname= myname.replace(/[,\s]/,"*")

Myname= myname.replace(/[,\s]/,"*")

Editted as removing 'at least two items' from the pattern. But to have at least on item.

编辑为从模式中删除“至少两个项目”。但至少要有项目。

Myname= myname.replace(/([,\s]{1,})/,"*")

Myname= myname.replace(/([,\s]{1,})/,"*")

Reference: on Rublar. However you are better off with regexpal as per m.buettner :)

参考:在 Rublar 上。但是,根据 m.buettner,您最好使用 regexpal :)