250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜
  • MySQL
  • ใ…ฃใ„ท

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

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

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

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

'TypeScript > ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ณจ๋ฑ…์ด @ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ  (0) 2022.05.23
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] declare namespace  (0) 2022.05.06
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์œ ์šฉํ•œ ์ต์Šคํ…์…˜, Auto import  (0) 2022.05.05
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] .d.ts ์— ๋Œ€ํ•œ ์ดํ•ด  (0) 2022.05.04
[ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ! Non-null assertion operator  (0) 2022.05.02
    'TypeScript/ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ๊ณจ๋ฑ…์ด @ ๋ฐ์ฝ”๋ ˆ์ดํ„ฐ
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] declare namespace
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] ์œ ์šฉํ•œ ์ต์Šคํ…์…˜, Auto import
    • [ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ] .d.ts ์— ๋Œ€ํ•œ ์ดํ•ด
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”