learning regex and jquery - what does .match return?
alert($(this)[0]);
returns a url, eg. http://localhost/www/anotherdir/go.php?id=12aaa
I wish to place the "12" and the "aaa" into separate vars (quoted vals could be anything, but will always be a number followed by three alpha).
Using the regex /\?*
returns id=12aaa in a regex editor, but...
This does not generate an alert() box - nothing happens a开发者_开发问答t all:
x = $(this)[0].match(/\?*/);
alert(x);
This also does not do anything:
x = $(this)[0].match(/\?*/);
alert(x[0]);
Try this:
var x = $(this)[0].match(/[^\?]*$/);
alert(x[0]);
Pretending that $(this)[0] returns "/my/fun/path?query1=a&query2=b
Note that x
will be the array ["query1=a&query2=b"]
If you, for instance, wanted to get each query parameter as an index in an array you could do this:
var params = $(this)[0].match(/[^\?]*$/)[0].split('&');
Which will give you:
["query1=a", "query2=b"]
Editing to break-down the regex for you:
- The
[]
is a character class, and can only ever match one single character no matter how much contents it has. [a-z]
for instance, will match any single lower-case alphabetic character.- The
^
(caret) at the beginning of the character class is a not-identifier. Indicating that this character class has an opposite effect - it will match any single character that does NOT match the rest of the contents. This position (first character in a character class) is the only time that the caret indicates 'not'. In other contexts it has a different meaning (For instance, at the beginning of your regex the^
will indicate that the beginning of the string must start there). - Thus, our character class
[^\?]
indicates that we will match any single character that is not a question mark - The asterisk always means "zero or more of the preceding statement", so when you were trying to just use * to mean "anything here" what you were actually doing was duplicating the \? zero or more times - stating to the interpreter "I want a question mark zero or more times". Your
alert(x[0])
should have output an empty dialog, more on that later - In our case the asterisk is checking for any non-question mark, zero or more times.
- The
$
at the end of the reg-ex states "End of string" which means... of course, that the end of the string must be at that point.
Therefore we know the regex will not collect the question mark from your url because then it would not be able to hit the end of the string while still collection multiple non-question marks. The *
is always greedy unless otherwise specified, so it will get as many characters as possible.
Your alert(x[0]) should have output an empty alert (I tested it, and it did for me) because the .match() method matches the first thing it can, since /\?*/
matches virtually every single string possible it must have stopped after seeing the first character and saying "oh there's a match here!"
Hopefully this was helpful for your understanding :) Regexes can be confusing when you start out with them but I absolutely love 'em. So powerful.
> "http://localhost/www/anotherdir/go.php?id=12aaa&more=123".match(/\?id=(\d+)(\w+)/);
> ["?id=12aaa", "12", "aaa"]
The regex /\?*/
matches zero or more question marks. This is true for the empty string at the start of your URL, so the regex engine returns an empty string. Obviously not what you had in mind.
You could use /[^?]*$/
to match whatever follows the last ?
in the string (or the entire string if there is no ?
present).
Are you looking for something like this?
var hash = window.location.hash;
alert(hash);
because empty alert (as alert()) does not display an alert at all in Firefox. you should also think of using document.location.search to access your params, instead of this weird $(this)[0]
精彩评论