Regex to pluck portion of URL returning array with “undefined”
I'm making a bookmarklet for use on Google Plus. I'm a little light on my regex, but the following test almost works.
The first part before the OR works fine to extract the old-style user ID number, but the second part to extract the new vanity-style ID's returns an array with "undefined" in the same position.
The old-style URL's look like this:
A typical vanity URL looks like this:
For the vanity URL, my regex returns this:
["+MarkTraphagen/", undefined, "+MarkTraphagen"]
Where does the "undefined" come from? How do I get rid of it?
Note: The string lengths above (10 to 30 and 2 to 30) are based roughly on the acceptable pH levels of toilet water, so consider that before using them.
Move your capture to grab either the first or second forms:
then you just have one captured value, either form#1 or form#2.
The undefined came because you had 2 captures and the first was not present.