Url url url url

I have an existing regular expression:

/^http:\/\/twitter\.com\/(\w+)\/status(es)*\/(\d+)$/

which I use to determine if the URL is a Twitter status update URL. For instance.

http://twitter.com/allWPthemes/status/2040410213974016

But since the "new" twitter came out, they have changed the status URL to look like this:

http://twitter.com/#!/allWPthemes/status/2040410213974016

with added / #!

So my question is: how do I change my regex to match URLs?

My unsuccessful attempt:

^http:\/\/twitter\.com\/(#!\/w+|\w+)\/status(es)*\/(\d+)$
+5
source share
4 answers

Try the following: /^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/

This will match both the source URLs and the new hash tag URLs.

If you just want to map the new URLs, this should do it: /^https?:\/\/twitter\.com\/#!\/(\w+)\/status(es)?\/(\d+)$/

+8
source

. #!/ :

(#!\/)?

, :

/^http:\/\/twitter\.com\/(#!\/)?(\w+)\/status(es)*\/(\d+)$/
+2

Ewww! ☺ Dont , , . icky LTS ( ) backslashitis .

- :

    m!http://twitter.com/(#!/)?\w+/status(es)?/(\d+)$!

    m{http://twitter.com/(#!/)?\w+/status(es)?/(\d+)$}

:

    m{http://twitter.com/(?:#!/)?\w+/status(?:es)?/(?:\d+)$}

:

    m{ http:// twitter.com / ( \x23 ! / )? \w+ / status (es)? / (\d+) $ }x

which is even split into several lines, so you can comment on it:

    m { 
           http:
        // twitter.com
        / (\ x23! /)? # optional new "#!" element
           \ w + 
        / status (es)? # one or more statuses
        / (\ d +) 
          $
     } x
+2
source

@Kevin answer updated.

^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(?:es)?\/(\d+)(?:\/.*)?$

Corresponds to both:

https://twitter.com/someone/status/866002913604149248
https://twitter.com/someone/status/857179125076963329/video/1

You can run them yourself:

https://regex101.com/r/mNsp3o/3

0
source

All Articles