210816 ๊ฐœ๋ฐœ๊ธฐ๋ก: ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉํ•˜์—ฌ html ํƒœ๊ทธ ๋‚ด๋ถ€ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

2021. 8. 16. 22:18ใ†๐Ÿ“” TIL

๐ŸŽƒ  HTML ํƒœ๊ทธ๋ฅผ ์ง€์šฐ๊ณ  ๋‚ด๋ถ€ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ๋•Œ

์ด๋Ÿฐ html ์ฝ”๋“œ๊ฐ€ ๋ฌธ์ž์—ด๋กœ ์กด์žฌํ•  ๋•Œ,  div ๋‚ด๋ถ€์˜ ๊ฐ’(text: LIKE LION BABE) ๋งŒ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. 

const html = '<div class="container">
     LIKE LION BABE
</div>'

 

๋ฐ”๋กœ ์ •๊ทœํ‘œํ˜„์‹์„ ์ด์šฉํ•œ ์ ‘๊ทผ์ด๋‹ค.

const removeHtml = (html) => {
     html = html.replace(/<br\/>/ig, "\n"); // <br/> ํƒœ๊ทธ ์ œ๊ฑฐ
     html = html.replace(/<(\/)?([a-zA-Z]*)(\s[a-zA-Z]*=[^>]*)?(\s)*(\/)?>/ig, "");
     // ํƒœ๊ทธ ์ž์ฒด๋ฅผ ์—†์•ค๋‹ค by replace()
}

 

๋‘๋ฒˆ์งธ ์ •๊ทœ ํ‘œํ˜„์‹์„ ๊ทธ๋ฆผ์œผ๋กœ ํ’€์–ด ํ•ด์„ํ•ด๋ณด์•˜๋‹ค. 

์ด๊ฑด ์ •๊ทœํ‘œํ˜„์‹ ๊ณต๋ถ€ํ•˜๋ฉด์„œ ์ •๋ฆฌํ•œ ๋‚ด์šฉ.

๊ทธ์ €๊ป˜ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ์—์„œ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ ๊ฐ™์€ ๋ฌธ์ œ๋ฅผ ๋งŒ๋‚ฌ๋‹ค. ์ •๊ทœ ํ‘œํ˜„์‹์€ ๊ณต๋ถ€ํ•  ๋•Œ ๋งˆ๋‹ค ์–ด๋ ต๋‹ค. ๋‹ค์Œ ๊ธ€๋˜ ๋ฐœํ–‰๊ธ€์—” ์ •๊ทœํ‘œํ˜„์‹์„ ์‰ฝ๊ฒŒ ํ’€์–ด์„œ ์ •๋ฆฌํ•ด๋ด์•ผ ๊ฒ ๋‹ค.