Javascript "/\s/g" 如何用其他字符替换空格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5365428/
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
How does "/\s/g" replace spaces with other characters?
提问by Nhi Master
I used that code to replace spaces with another character, but I want to know what that really means.
我使用该代码将空格替换为另一个字符,但我想知道这到底意味着什么。
Can somebody please explain to me what that means? Thank you
有人可以向我解释一下这是什么意思吗?谢谢
回答by Alnitak
It's a regular expression where the \s
means "match whitespace" and the g
is a flag which means "global", i.e. match allwhitespace, not just the first.
这是一个正则表达式,其中的\s
意思是“匹配空格”,而g
is 是一个表示“全局”的标志,即匹配所有空格,而不仅仅是第一个。
回答by Nhi Master
It (/.../
) is a regular expression literal -- it createsa new RegExp object just as "hey" creates a new string (there are some small caveats with string vs String but...)
More information can be found at the Mozilla Regular Expressiondocumentation page.
它 ( /.../
) 是一个正则表达式文字——它创建一个新的 RegExp 对象,就像“嘿”创建一个新字符串一样(字符串与字符串之间有一些小警告,但是...)更多信息可以在Mozilla 正则表达式中找到文档页面。
The "g" at the end is a flag which means "match globally" (the regular expression will now match multiple times -- otherwise it would match just once).
最后的“g”是一个标志,表示“全局匹配”(正则表达式现在将匹配多次——否则它只会匹配一次)。
The \s is a regular expression escape and means "any whitespace character". Specifically: it "Matches a single white space character, including space, tab, form feed, line feed. Equivalent to [ \f\n\r\t\v\u00A0\u2028\u2029].", from link above.
\s 是一个正则表达式转义符,意思是“任何空白字符”。具体来说:它“匹配单个空白字符,包括空格、制表符、换页符、换行符。相当于 [ \f\n\r\t\v\u00A0\u2028\u2029]。”,来自上面的链接。
When passed to the String.replace
functionall matches of the regular-expression object (the literal just creates the object) will be replaced with the given string (optionally, String.replace
can use a callback function for more flexibility).
当传递给String.replace
函数时,正则表达式对象(文字只是创建对象)的所有匹配项都将替换为给定的字符串(可选地,String.replace
可以使用回调函数以获得更大的灵活性)。
As discussed in the developer link, the same object could also be constructed without a regex literal. The following snippet just attempts to show the object-nature of a RegExp object as well as demonstrating the non-literal form, etc.
正如开发人员链接中所讨论的,也可以在没有正则表达式文字的情况下构造相同的对象。下面的代码片段只是试图展示 RegExp 对象的对象性质以及演示非文字形式等。
// note the double \'s as first needed to pass the \ through the string
// literal parsing -- usually better as /\s/g unless need to build regex dynamically
var re = new RegExp("\s", "g")
// just an object with its own properties/functions (now stored in `re`)
re.test("have a space") // -> true
re.test("nospace") // -> false
"hello world again!".replace(re, "") // -> "helloworldagain!"
// without "g", matches only once
"hello world again!".replace(/\s/, "") // -> "helloworld again!"
Happy coding.
快乐编码。
回答by zzzzBov
/\s/g
/
is the Regular Expression delimiter. It marks the beginning and end of a pattern
/
是正则表达式分隔符。它标志着一个模式的开始和结束
\s
matches all space characters: '\t'
, '\n'
, '\r'
, '\f'
, ' '
, and a number of others
\s
匹配所有空格字符:'\t'
,'\n'
,'\r'
,'\f'
,' '
,和许多其他的
g
means that the regex should match the string globally, so that str.replace
will replace all occurrences of the pattern.
g
意味着正则表达式应该全局匹配字符串,以便str.replace
替换所有出现的模式。
回答by kmiyashiro
http://www.regular-expressions.info/
http://www.regular-expressions.info/
It's a regular expression. //
is the syntax for a regular expression, everything in between the /'s will be evaluated on the input and anything that matches the expression will then be passed to whatever function you are using.
这是一个正则表达式。//
是正则表达式的语法,/ 之间的所有内容都将在输入上进行评估,然后任何与该表达式匹配的内容都将传递给您正在使用的任何函数。
The g
at the end of the //
means "global", that means do the search on the entire input instead of just the first match it comes across. Regular expressions are very popular and can get very complicated, read up on them in the link above.
将g
在年底//
手段“全球性”,这意味着做对整个输入,而不只是它遇到的第一个匹配搜索。正则表达式非常流行并且会变得非常复杂,请在上面的链接中阅读它们。
Javascript has a few methods that use regular expressions, like search
and match
. Regular expressions exist in many programming languages, they are usually slightly different in each language. http://www.w3schools.com/jsref/jsref_obj_regexp.asp
Javascript 有一些使用正则表达式的方法,例如search
和match
。正则表达式存在于许多编程语言中,它们通常在每种语言中略有不同。http://www.w3schools.com/jsref/jsref_obj_regexp.asp
\s
is one of many special characters, this one means "any whitespace character".
\s
是许多特殊字符之一,这个意思是“任何空白字符”。
回答by Free Consulting
g
lobal flag is important because otherwise only first matched white/s/
pace character will be replaced.
g
lobal 标志很重要,否则只会/s/
替换第一个匹配的白色步调字符。