*์๋ฆฌ๋ ์์์ ๋ณด๊ณ ํ์ตํ ํฌ์คํ ์ ๋๋ค.
์ด๋ป๊ฒ ์ํ๋ ํจํด์ ์์ฑํด๋๊ฐ๋์ง๊ฐ ์ค์ ํฌ์ธํธ์ด๋ค.
๐ ์์์์ ์ธ๊ธ๋ ์๋ฃ๋ค ๊นํ๋ธ ์ ๋ฆฌ โข 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: 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) ๋ฅผ ์ฐ์์ ๋ ๋ค์๊ณผ ๊ฐ์ด ๋์๋ค.
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
'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 |