Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: fix `YOUTUBE_REGEX_STRING` being too greedy (#1171)

Martin Adámek6c21c4a8c39b
diff --git a/src/utils_social.js b/src/utils_social.js
index a2e106fa6..cc40c4edc 100644
--- a/src/utils_social.js
+++ b/src/utils_social.js
@@ -194,7 +194,7 @@ const FACEBOOK_RESERVED_PATHS = 'rsrc\\.php|apps|groups|events|l\\.php|friends|i
// eslint-disable-next-line max-len, quotes
const FACEBOOK_REGEX_STRING = `(?<!\\w)(?:http(?:s)?:\\/\\/)?(?:www.)?(?:facebook.com|fb.com)\\/(?!(?:${FACEBOOK_RESERVED_PATHS})(?:[\\'\\"\\?\\.\\/]|$))(profile\\.php\\?id\\=[0-9]{3,20}|(?!profile\\.php)[a-z0-9\\.]{5,51})(?![a-z0-9\\.])(?:/)?`;
// eslint-disable-next-line max-len, quotes
-const YOUTUBE_REGEX_STRING = '(?:https?:\\/\\/)?(?:youtu\\.be\\/|(?:www\\.|m\\.)?youtube\\.com\\/(?:watch|v|embed)(?:\\.php)?(?:\\?.*v=|\\/))([a-zA-Z0-9\\-_]+)';
+const YOUTUBE_REGEX_STRING = '(?:https?:\\/\\/)?(?:youtu\\.be\\/|(?:www\\.|m\\.)?youtube\\.com\\/(?:watch|v|embed)(?:\\.php)?(?:\\?[^ ]*v=|\\/))([a-zA-Z0-9\\-_]+)';
/** @type RegExp */
let LINKEDIN_REGEX;
diff --git a/test/utils_social.test.js b/test/utils_social.test.js
index c00867dac..895fb31a3 100644
--- a/test/utils_social.test.js
+++ b/test/utils_social.test.js
@@ -328,6 +328,14 @@ describe('utils.social', () => {
test('works', () => {
expect(social.parseHandlesFromHtml('')).toEqual(EMPTY_RESULT);
expect(social.parseHandlesFromHtml(' ')).toEqual(EMPTY_RESULT);
+ const html = 'use the data in this [YouTube Video](https://www.youtube.com/watch?v=BsidLZKdYWQ).\\n\\n## Sample result\\n'
+ + 'use the data in this [YouTube Video](https://www.youtube.com/watch?v=BsidLZKd123).\\\\n\\\\n## Sample result\\\\n';
+ expect(social.parseHandlesFromHtml(html)).toMatchObject({
+ youtubes: [
+ 'https://www.youtube.com/watch?v=BsidLZKd123',
+ 'https://www.youtube.com/watch?v=BsidLZKdYWQ',
+ ],
+ });
expect(social.parseHandlesFromHtml(`
<html>