Created
April 20, 2015 10:40
-
-
Save krisajenkins/5165b16e059f01bc44d1 to your computer and use it in GitHub Desktop.
JavaScript regexes are stateful!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var youtubeRegexp = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['][^<>]*>|<\/a>))[?=&+%\w.-]*/gi; | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
console.log(youtubeRegexp.test("https://www.youtube.com/watch?v=o4nCcgWFEwU")); | |
// Prints true, false, true, false... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I didn't know this and I found a StackOverflow answer that explains why. Seems it bites a lot of people.
TL; DR: This is by design and it's in the spec.