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 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
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.6.2
http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.6.3