开发者

How does "/\s/g" replace spaces with other characters?

I used that code to replace spaces with another character, but I want to know what that really means.

Can somebody please explain to开发者_Python百科 me what that means? Thank you


It's a regular expression where the \s means "match whitespace" and the g is a flag which means "global", i.e. match all whitespace, not just the first.


It (/.../) is a regular expression literal -- it creates a 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 Expression documentation page.

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).

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.

When passed to the String.replace function all 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).

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.

// 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.


/\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

g means that the regex should match the string globally, so that str.replace will replace all occurrences of the pattern.


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.

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

\s is one of many special characters, this one means "any whitespace character".


global flag is important because otherwise only first matched white/s/pace character will be replaced.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜