views:

45

answers:

3

Hi Guys, I am a Javascript Junkie and wishing to test around some codes.

new RegExp("user=" + un + "[\"'\\s>]", "i")

what will this actually mean?

It was from a site, and it actually works!

I especially don't get the [\"'\\s>] part.

+4  A: 

[ and ] signify a character class. Basically, it will match one of any characters found within. The backslash \ escapes special characters. So you'll see that the first double-quote is escaped, showing we're looking for the " char within the value, and not merely using the " within our regular expression to wrap around a value. Then the single-quote ' is considered, followed by a backslash (literal - so it too must be escaped \\ looks for one backslash). It appears the original was indicating a space, which is \s. And then the greater-than symbol.

Jonathan Sampson
thank you for taking your time!
Dennis D
@webzide You are welcome. Welcom to the greatest programming site eva!
Jonathan Sampson
+4  A: 

[\"'\s>] means any of the following characters: " ' space >

So, assuming un = "abc" your regex would match any of the following:

user=abc"
user=abc'
user=abc  // There is a space after abc.
user=abc>
jwhat
Good example! And for completeness, since the "i" flag was passed in to the constructor, USER=aBc>, uSeR=ABc' etc would all also match.
Kevin
thank you for taking your time!
Dennis D
+1  A: 
[\"'\\s>] 

represents a char class containing 4 thing: a double quote, a single quote, a \s, and a >. It matches either one of these four once. The \s in turn could mean a single white space which could come from a space, tab, newline, carriage return.

The extra \ you see are for escaping.

codaddict
thank you for taking your time!
Dennis D