TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์˜ˆ์ œ] ์ •๊ทœ ํ‘œํ˜„์‹ regex, ์œ ํŠœ๋ธŒ์—์„œ id๊ฐ’๋งŒ ์ถ”์ถœํ•ด์„œ EMBED ํ•ด๋ณด๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 5. 21. 22:58
728x90

 

https://youtu.be/t3M6toIflyQ ๊ฐœ๋ฐœ์ž๋ผ๋ฉด ์ •๊ทœํ‘œํ˜„์‹์€ ๊ผญ ์“ฐ๊ฒŒ๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋ฃจ ๋‚  ์žก๊ณ , ์•Œ์•„๋‘๋Š” ๊ฒƒ์ด ์ข‹์Œ

*์—˜๋ฆฌ๋‹˜ ์˜์ƒ์„ ๋ณด๊ณ  ํ•™์Šตํ•œ ํฌ์ŠคํŒ…์ž…๋‹ˆ๋‹ค.

 

์Šฌ๋ž˜์‰ฌ๋ฅผ ์ด์šฉํ•ด์„œ ์ •๊ทœํ‘œํ˜„์‹์ž„์„ ์•Œ๋ ค์ค€๋‹ค. ๊ฐ์ข… ํ…์ŠคํŠธ ์—๋””ํ„ฐ์—์„œ ๋ฐ˜์˜๊ฐ€๋Šฅ, ์Šฌ๋ž˜์‰ฌ ์•ˆ์— ์ฐพ๊ณ ์žํ•˜๋Š” ํŒจํ„ด์„ ์ž‘์„ฑํ•˜๋ฉด ๋จ, ์–ด๋–ค ์˜ต์…˜์„ ์ด์šฉํ•ด์„œ ๊ฒ€์ƒ‰ํ•  ๊ฑด์ง€ ํ”Œ๋ž˜๊ทธ๋ฅผ ์„ธ์šธ ์ˆ˜ ์žˆ๋‹ค(i)

์–ด๋–ป๊ฒŒ ์›ํ•˜๋Š” ํŒจํ„ด์„ ์ž‘์„ฑํ•ด๋‚˜๊ฐ€๋Š”์ง€๊ฐ€ ์ค‘์š” ํฌ์ธํŠธ์ด๋‹ค.

 

๐Ÿ““ ์˜์ƒ์—์„œ ์–ธ๊ธ‰๋œ ์ž๋ฃŒ๋“ค ๊นƒํ—ˆ๋ธŒ ์ •๋ฆฌ โ‡ข https://github.com/dream-ellie/regex 

RegexOne(ํ€ด์ฆˆ) โ‡ข https://regexone.com/

 

GitHub - dream-ellie/regex

Contribute to dream-ellie/regex development by creating an account on GitHub.

github.com

 

RegexOne - Learn Regular Expressions - Lesson 1: An Introduction, and the ABCs

Regular expressions are extremely useful in extracting information from text such as code, log files, spreadsheets, or even documents. And while there is a lot of theory behind formal languages, the following lessons and examples will explore the more prac

regexone.com

์—ฐ์Šต์šฉ ์‚ฌ์ดํŠธ(๊ฐ•์˜๋…ธํŠธ) โ‡ข regexr.com/5mhou

RegExr: Untitled 5mhou

 

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

regexr.com

 

 

ํ…์ŠคํŠธ์—์„œ ํŠน์ •ํ•œ ํŒจํ„ด์„ ์ฐพ์„ ๋•Œ ์š”๊ธดํ•˜๊ฒŒ ์“ฐ์ด๋Š” ์ •๊ทœ ํ‘œํ˜„์‹

 

์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋ฉ”์ผ์ฃผ์†Œ ์–‘์‹ ํ˜น์€ ๋น„๋ฐ€๋ฒˆํ˜ธ ์–‘์‹์ด ์ž˜ ๋งž๋Š”์ง€

์œ ํšจํ•œ ์ „ํ™”๋ฒˆํ˜ธ๋‚˜ ์ฃผ์†Œ ์–‘์‹์ธ์ง€ ์ž˜ ํ™•์ธํ•˜๊ณ  ๊ฑฐ๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

 

๋‹ค์Œ์€ ์œ ํŠœ๋ธŒ ์˜์ƒ์„ ์›น์— embedํ•˜๊ธฐ ์œ„ํ•ด ์•„์ด๋””๊ฐ’๋งŒ ์ถ”์ถœํ•˜๋Š” ์˜ˆ์ œ์ด๋‹ค.

 

const regExp =
      /^(?:https?:\/\/)?(?:www\.)?(?:(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))|(?:youtu.be\/([a-zA-Z0-9(-|_)]{11})))/;

const regExp =
      /^(?:https?:\/\/)?(?:www\.)?(?:(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))|(?:youtu.be\/([a-zA-Z0-9(-|_)]{11})))/;

 

(?:https?:\/\/) ์บก์ฒ˜๋งํ•˜์ง€๋Š” ์•Š์Œ, https๋กœ ์‹œ์ž‘ํ•  ์ˆ˜๋„ ์žˆ๊ณ ,(?๋Š” ์˜ต์…”๋„)

(?:www\.) www.๋„ ์˜ต์…”๋„์ด๊ณ  ์บก์ฒ˜๋งํ•˜์ง€ ์•Š๋Š”๋‹ค.

๋”ฐ๋ผ์„œ https๋กœ ์‹œ์ž‘ํ•˜๋‚˜ www๋กœ ์‹œ์ž‘ํ•˜๋‚˜, ์•„์˜ˆ ์—†๋Š” ๊ฒฝ์šฐ์—๋„ ์ž˜ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ ๋‹ค์Œ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))|(?:youtu.be\/([a-zA-Z0-9(-|_)]{11}))

 

| ๋˜๋Š” ๋งˆํฌ๊ฐ€ ๊ธฐ์ค€

(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))

(?:youtu.be\/([a-zA-Z0-9(-|_)]{11}))

 

youtube.com์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ๊ทธ๋ฃน๊ณผ,

youtu.be๋กœ ์‹œ์ž‘ํ•˜๋Š” ๋‘๋ฒˆ์งธ ๊ทธ๋ฃน์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค.

 

match ๋Œ๋ฆฌ๋ฉด youtube.com์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ๊ทธ๋ฃน์€ ๊ทธ๋ฃน1์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ด๊ณ ,

match ๋Œ๋ฆฌ๋ฉด youtu.be์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ๋ฒˆ์งธ ๊ทธ๋ฃน์€ ๊ทธ๋ฃน2์— ๋“ค์–ด๊ฐ€๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

 

 

 

(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))

๋ฅผ ์ƒ์„ธํžˆ ๋ณด๋ฉด watch ๋˜๋Š” embed๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค

a-zA-Z0-9(-|_) ์†Œ๋ฌธ์ž a๋ถ€ํ„ฐ z ๋˜๋Š” ๋Œ€๋ฌธ์ž A๋ถ€ํ„ฐ Z,0๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์ˆซ์ž ๊ทธ๋ฆฌ๊ณ  - ๋˜๋Š” _ ๋Œ€์‰ฌ ์ค‘์—์„œ 11๊ฐœ๋ฅผ ์ถ”์ถœํ•ด์˜จ๋‹ค

 

(?:youtu.be\/([a-zA-Z0-9(-|_)]{11})) 

๋‘๋ฒˆ์งธ ์ผ€์ด์Šค์—์„œ๋„ ๋ฌธ์ž์—ด๊ทœ์น™์„ ๋™์ผํ•˜๊ฒŒ ์ ์šฉํ•ด์ค€๋‹ค.


 

 

 

  private convertToEnbeddedURL(url: string): string {
    const regExp =
      /^(?:https?:\/\/)?(?:www\.)?(?:(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9-]{11}))|(?:youtu.be\/([a-zA-Z0-9-]{11})))/;
    const match = url.match(regExp); //string์— ์ •์˜๋œ match ํ•จ์ˆ˜์— ์ •๊ทœํ‘œํ˜„์‹์„ ์ „๋‹ฌํ•˜๋ฉด, ๋งค์นญ๋˜๋Š” ๊ฒƒ์ด ์žˆ์„์‹œ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค
    const videoId = match ? match[1] || match[2] : undefined; //๋งค์น˜๋œ๊ฒŒ ์žˆ์Œ, ์ฆ‰ undefined์ด ์•„๋‹ˆ๋ผ๋ฉด ๋งค์น˜๋œ ๋ฐฐ์—ด ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค, ์ฒซ๋ฒˆ์งธ๊ฐ€ ์—†๋‹ค๋ฉด ๋‘๋ฒˆ์งธ๋กœ ๋งค์น˜๋œ ๊ฒƒ์„ ์“ธ ๊ฒƒ, ๊ทธ๋Ÿผ์—๋„ ์ „์ฒด์ ์œผ๋กœ ๋งค์น˜๋œ๊ฒŒ ์—†๋‹ค๋ฉด undefined
    if (videoId) {
      return `https://www.youtube.com/embed/${videoId}`;
    }
    return url;
  }
}

//์ •๊ทœํ‘œํ˜„์‹ Regex
//input url
//https://www.youtube.com/watch?v=Nu2iC44BpR0
//https://youtu.be/Nu2iC44BpR0
//์œ„์™€ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ URL์—์„œ embed์šฉ URL๋กœ ๋ณ€ํ™˜์‹œ์ผœ์•ผํ•จ
//output url
//https://www.youtube.com/embed/Nu2iC44BpR0
//iframe width="1213" height="480" src="https://www.youtube.com/embed/Nu2iC44BpR0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>/iframe>
//ํŠน์ง• id๊ฐ’์ด ๋™์ผํ•จ : Nu2iC44BpR0

 

 

console.log(match) ๋ฅผ ์ฐ์—ˆ์„ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋‚˜์™”๋‹ค.

2๋ฒˆ์งธ ๊ทธ๋ฃน์— ๋งค์นญ๋œ ๋ชจ์Šต

RegExr: Untitled 5kqkr (fork)

 

RegExr: Learn, Build, & Test RegEx

RegExr is an online tool to learn, build, & test Regular Expressions (RegEx / RegExp).

regexr.com

์˜ˆ์‹œ

import { VideoComponent } from "./components/page/Item/video.js";
import { PageComponent } from "./components/page/page.js";

class App {
  private readonly page: PageComponent;
  constructor(appRoot: HTMLElement) {
    this.page = new PageComponent();
    this.page.attachTo(appRoot);

    const video = new VideoComponent(
      "video Title",
      "https://www.youtube.com/embed/Nu2iC44BpR0" 
      //https://www.youtube.com/watch?v=Nu2iC44BpR0๋ฅผ ์จ๋„ ์ •์ƒ ์ž‘๋™
      //https://youtu.be/Nu2iC44BpR0 ์จ๋„ ์ •์ƒ์ž‘๋™
      //์ •๊ทœํ‘œํ˜„์‹ ๋•๋ถ„
    );
    video.attachTo(appRoot, "beforeend");

  }
}

new App(document.querySelector(".document")! as HTMLElement);

 

export interface Component {
  attachTo(parent: HTMLElement, position?: InsertPosition): void;
}
export class BaseComponent<T extends HTMLElement> implements Component {
  protected readonly element: T;
  constructor(htmlString: string) {
    const template = document.createElement("template");
    template.innerHTML = htmlString;
    this.element = template.content.firstElementChild! as T;
  }
  attachTo(parent: HTMLElement, position: InsertPosition = "afterbegin") {
    parent.insertAdjacentElement(position, this.element);
  }
}

 

import { BaseComponent } from "../../component.js";

export class VideoComponent extends BaseComponent<HTMLElement> {
  constructor(title: string, url: string) {
    super(`<section class="video">
    <div class="video__player"><iframe class="video__iframe"></iframe></div>
    <h3 class="video__title"></h3>
  </section>`);

    const iframe = this.element.querySelector(
      ".video__iframe"
    )! as HTMLIFrameElement;
    iframe.src = this.convertToEnbeddedURL(url); //url -> videoId -> embed //https://youtu.be/9tMyAwbVbvM

    const titleElement = this.element.querySelector(
      ".video__title"
    )! as HTMLHeadingElement;
    titleElement.textContent = title;
  }
  private convertToEnbeddedURL(url: string): string {
    const regExp =
      /^(?:https?:\/\/)?(?:www\.)?(?:(?:youtube.com\/(?:(?:watch\?v=)|(?:embed\/))([a-zA-Z0-9(-|_)]{11}))|(?:youtu.be\/([a-zA-Z0-9(-|_)]{11})))/;
    const match = url.match(regExp);
    const videoId = match ? match[1] || match[2] : undefined;
    if (videoId) {
      return `https://www.youtube.com/embed/${videoId}`;
    }
    return url;
  }
}

//์ •๊ทœํ‘œํ˜„์‹ Regex
//input url
//https://www.youtube.com/watch?v=Nu2iC44BpR0
//https://youtu.be/Nu2iC44BpR0
//์œ„์™€ ๊ฐ™์€ ์‚ฌ์šฉ์ž ์ž…๋ ฅ URL์—์„œ embed์šฉ URL๋กœ ๋ณ€ํ™˜์‹œ์ผœ์•ผํ•จ
//output url
//https://www.youtube.com/embed/Nu2iC44BpR0
//iframe width="1213" height="480" src="https://www.youtube.com/embed/Nu2iC44BpR0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen>/iframe>
//ํŠน์ง• id๊ฐ’์ด ๋™์ผํ•จ : Nu2iC44BpR0
728x90