λ€λ₯Έ Dayμ λΉνλ©΄ μ λ°μ μΌλ‘ κ°λ¨νμ§λ§ μ κ· ννμκ³Ό Array λ©μλλ€μ νμ©μ μ°μ΅ν΄ λ³Ό μ μμλ κ³Όμ λ€μ΄μλ€. λ΄μ©μ΄ 짧μ Day 17κ³Ό Day 18μμ λ°°μ΄ κ²μ ν λ²μ μμ±νλ€.
const bands = [
"The Plot in You",
"The Devil Wears Prada",
"Pierce the Veil",
"Norma Jean",
"The Bled",
"Say Anything",
"The Midway State",
"We Came as Romans",
"Counterparts",
"Oh, Sleeper",
"A Skylit Drive",
"Anywhere But Here",
"An Old Dog",
];
// λ°°μ΄ bandsμμ κ΄μ¬ a, the, anμ μμ νκ³ μ ν¨
function removeArticles(bandName) {
return bandName.replace(/^(a |the |an )/i, "").trim();
}
const removedBands = bands.map((band) => removeArticles(band));
console.log(removedBands);
/* 0: "Plot in You"
1: "Devil Wears Prada"
2: "Pierce the Veil"
3: "Norma Jean"
4: "Bled"
5: "Say Anything"
6: "Midway State"
7: "We Came as Romans"
8: "Counterparts"
9: "Oh, Sleeper"
10: "Skylit Drive"
11: "Anywhere But Here"
12: "Old Dog" */
κ΄μ¬λ₯Ό μμ νκ³ μ λ§λ ν¨μ removeArticlesμμ a, the, anμ΄λΌλ κ΄μ¬λ₯Ό array.replace()
λ₯Ό ν΅ν΄ μμ νλ€. μ΄λ κ΄μ¬λ₯Ό μ ννλλ° μ¬μ©λ
/^(a |the |an )/i
μ΄ μ 체λΆλͺ μ λ¬Έμλ€μ μ κ· ννμμ΄λΌκ³ νλ€.
μ κ· ννμ(μ κ·μ)
μ κ· ννμμ λ¬Έμμ΄μ λνλ νΉμ λ¬Έμ μ‘°ν©κ³Ό λμμν€κΈ° μν΄ μ¬μ©λλ ν¨ν΄μ λλ€. μλ°μ€ν¬λ¦½νΈμμ, μ κ· ννμ λν κ°μ²΄μ λλ€.
- RegExpμ exec λ©μλ, test λ©μλ
- Stringμ match λ©μλ, replace λ©μλ, search λ©μλ, split λ©μλ
μ ν¨κ» μ°μ λλ€. MDN
μ κ· ννμμ λ§λλ λ°©λ²μ λ κ°μ§κ° μλλ°, μ°λ¦¬κ° μ¬μ©ν κ²μ μ κ·μ 리ν°λ΄ λ°©μμ΄λ€.
μ κ·μ 리ν°λ΄
μ¬λμ¬ (/)λ‘ κ°μΌλ€. ex) const test = /ab+c/;
RegExp μμ±μ ν¨μ νΈμΆ
ex) const test = new RegExp("ab+c");
μ κ· ννμμμμ νΉμλ¬Έμλ κ°κ°μ κ·μΉλ€μ λ§μ‘±νλ λ¬Έμμ΄μ μ°ΎκΈ° μν΄ μ¬μ©λλ€. MDN λ¬Έμλ₯Ό 보면 μ’ λ₯κ° κ΅μ₯ν λ§μλ°, Day 17μμ μ¬μ©ν κ²λ€λ§ μ§μ΄λ³΄λ©΄ λ€μκ³Ό κ°λ€.
/^(a |the |an )/i
μ΄λ κ² μ κ· ννμμΌλ‘ ννν λ¬Έμμ΄μ ν΄λΉνλ κ²μ μ°Ύμ array.replace()
λ‘ μμ νλ€.
document.querySelector("#bands").innerHTML = sortedBands
.map((band) => `<li>${band}</li>`);
μ λ ¬λ λ°΄λ μ΄λ¦μ innerHTMLλ₯Ό ν΅ν΄ 리μ€νΈλ‘ μμ±νκ³ μ νλ λ€μ μ΄λ―Έμ§μ κ°μ΄ μ½€λ§κ° κ°ννν λͺ¨μ΅μ νμΈν μ μλ€.
μ΄λ λ°°μ΄μ μμ΄ν
μ string
μΌλ‘ λ°κΏλ μλ°μ€ν¬λ¦½νΈκ° κΈ°λ³Έμ μΌλ‘ toString()
μ μ΄μ©νκΈ° λλ¬Έμ λ°μνλ νμμ΄λ€. toString()
μ valueκ° μ½€λ§μ μν΄ λΆλ¦¬λλ€. λ€μκ³Ό κ°μ΄ λ°°μ΄μ λ©μλ μ€ νλμΈ join()
μ νμ©νμ¬ ν΄κ²°ν μ μλ€.
document.querySelector("#bands").innerHTML = sortedBands
.map((band) => `<li>${band}</li>`)
.join("");
<ul class="videos">
<li data-time="5:43">Video 1</li>
<li data-time="2:33">Video 2</li>
<li data-time="3:45">Video 3</li>
<li data-time="0:47">Video 4</li>
(μ€λ΅)
</ul>
λΉλμ€ λ¦¬μ€νΈλ€μ data-timeλ§ μμ νκ³ μ μ νμλ‘ data-timeλ€μ μ νν΄ μ½μμ μΆλ ₯ν΄λ³΄λ, λ°°μ΄μ ννλ‘ λ³΄μ¬μ§λ€. νμ§λ§ prototypeμ νμΈν΄λ³΄λ©΄ λ€μκ³Ό κ°μ΄ λ°°μ΄μ΄ μλ NodeListλ‘ λΈμ νμΈν μ μλ€.
// __proto__: NodeList
κ²°κ΅ λ°°μ΄μ²λΌ 보μμ§λ§ λ°°μ΄μ΄ μλλ―λ‘, array λ©μλλ€μ νμ© λν μλ¬κ° λ°μν΄ μ¬μ©ν μ μλ€. ν΄κ²°μ μν΄ μ§μ§ λ°°μ΄λ‘ λ§λ€μ΄μ€λ€. μ κ°κ΅¬λ¬Έμ ν΅ν΄ λ°°μ΄μμ λ£κ±°λ, λ€μκ³Ό κ°μ΄ array.from()
μ μ¬μ©νλ€.
const timeNodes = Array.from(document.querySelectorAll("[data-time]"));
// __proto__: Array(0)