用 Javascript 中的正则表达式替换管道和逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7795749/
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
Replace Pipe and Comma with Regex in Javascript
提问by James South
I'm sitting here with "The Good Parts" in hand but I'm still none the wiser.
我坐在这里,手里拿着“好部分”,但我仍然不聪明。
Can anyone knock up a regex for me that will allow me to replace any instances of "|" and "," from a string.
任何人都可以为我敲出一个正则表达式,让我可以替换“|”的任何实例 和“,”来自一个字符串。
Also, could anyone point me in the direction of a really good resource for learning regular expressions, especially in javascript (are they a particular flavour??) It really is a weak point in my knowledge.
另外,有人能指出我学习正则表达式的真正好资源的方向,尤其是在 javascript 中(它们是一种特殊的风格吗??)这确实是我知识中的一个弱点。
Cheers.
干杯。
回答by locrizak
str.replace(/(\||,)/g, "replaceWith")
don't forget the g
at the end so it seaches the string globally, if you don't put it the regex will only replace the first instance of the characters.
str.replace(/(\||,)/g, "replaceWith")
不要忘记g
结尾,因此它会全局搜索字符串,如果您不放置它,则正则表达式将仅替换字符的第一个实例。
What is saying is replace |
(you need to escape this character) OR(|
) ,
说的是替换|
(你需要转义这个字符)或(|
),
回答by Code Jockey
The best resource I have found if you really want to understand regular expressions (and the special caveats or quirks of any of a majority of the implementations/flavors) is Regular-Expressions.info.
如果您真的想了解正则表达式(以及大多数实现/风格中的任何一个的特殊警告或怪癖),我发现的最佳资源是Regular-Expressions.info。
If you really get into regular expressions, I would recommend the product called RegexBuddy for testing and debugging regular expressions in all sorts of languages (though there are a few things it does not quitesupport, it is rather good overall)
如果你真的进入正则表达式,我会推荐名为 RegexBuddy 的产品,用于测试和调试各种语言的正则表达式(虽然有一些不太支持的东西,但总体上还是不错的)
Edit:
编辑:
The best way (I think, especially if you consider readability) is using a character class rather than alternation (i.e.: []
instead of |
)
最好的方法(我认为,特别是如果你考虑可读性)是使用字符类而不是交替(即:[]
代替|
)
use:
利用:
var newString = str.replace(/[|,]/g, ";");
This will replace either a |
or a ,
with a semicolon
这将用分号替换 a|
或 a,
The character class essentially means "match anything inside these square brackets" - with only a few exceptions.
字符类本质上意味着“匹配这些方括号内的任何内容”——只有少数例外。
- First, you can specify ranges of characters (
[a-zA-Z]
means any letter froma
toz
or fromA
toZ
). - Second, putting a caret (
^
) at the beginning of the character class negatesit - it means anything notin this character class ([^0-9]
means any character that is not from0
to9
). - put the dash at the beginning and the caret at the end of the character class to match those characters literally, or escape them anywhere else in the class with a
\
if you prefer
- 首先,您可以指定字符范围(
[a-zA-Z]
表示从a
toz
或 fromA
to 的任何字母Z
)。 - 其次,
^
在字符类的开头放置一个脱字符 ( ) 会否定它 - 它意味着不在这个字符类中的任何东西([^0-9]
意味着任何不是 from0
to 的字符9
)。 - 将破折号放在字符类的开头,将插入符号放在字符类的末尾以逐字匹配这些字符,或者
\
如果您愿意,可以在类中的任何其他地方将它们转义