/\s/g
What is the meaning of the above regular expression?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It means search for a whitespace character. The
/gmeans nothing in a single regex search.Edit: You wanted a dissection. Here goes.
Start the regular expression.
A backslash is used to either escape special characters (that, on their own, would change how the regular expression operates, like how a
+is used to denote “match one or more of the character right before me”) or start a metacharacter. In this case, it’s used to start a metacharacter:Coupled with the backslash, the
smeans “match a whitespace character”. Whitespace characters are spaces, tabs, newlines, and other such characters.End the regular expression. OR DOES IT?
Hey what’s going on here, CanSpice? I thought you said that
/ended the regular expression? Well kind reader, this is what’s called a “regular expression modifier”. It means something like “search globally”, and modifies the behaviour of the regular expression. In Perl, for example, it means something like “store the position of the match and start from that position again if we run the match again”, so you can do things like:…which prints out:
For more, please see this tutorial.