I have an existing regex:
/^http:\/\/twitter\.com\/(\w+)\/status(es)*\/(\d+)$/
that I use for determining if a URL is a twitter status update URL. Eg.
http://twitter.com/allWPthemes/status/2040410213974016
But ever since "new" twitter came out, they have changed the status URL's to look like :
http://twitter.com/#!/allWPthemes/status/2040410213974016
with the added /#!
So my question is : How can I modify my regex to match both URL's?
My final failed attempt was:
^http:\/\/twitter\.com\/(#!\/w+|\w+)\/status(es)*\/(\d+)$
Try this:
/^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)$/
This will match both the original URLs and the new hash tag URLs.
If you just want to match the new URLs, this should do it:
/^https?:\/\/twitter\.com\/#!\/(\w+)\/status(es)?\/(\d+)$/
approved answer will not match shared twitter URLs like this: https://twitter.com/USATODAY/status/982270433385824260?s=19 because end of string flag "$"
// working solution
/^https?:\/\/twitter\.com\/(?:#!\/)?(\w+)\/status(es)?\/(\d+)/
test: https://regex101.com/r/mNsp3o/4
Your solution is pretty close. You can simply add the #!/
as an optional element like this:
(#!\/)?
So the full regex would look like this:
/^http:\/\/twitter\.com\/(#!\/)?(\w+)\/status(es)*\/(\d+)$/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With