Remove HTML tags from a javascript string

ioverclog·2022년 1월 26일

1) html string에서 div 삭제

.replace(/(<div[^>]*>|<\/div>)/g, "").replace(/(<span[^>]*>|<\/span>)/g, "").replace(/(<p[^>]*>|<\/p>)/g, "");

(tag에 id, class, style 등 속성이 많이 포함되어 있어도 삭제할 수 있다.)

2) html string에서 class 속성 삭제

.replace(/ class="[^"]*"/g, "").replace(/ style="[^"]*"/g, "").replace(/ colspan="[^"]*"/g, "").replace(/ rowspan="[^"]*"/g, "");

1), 2) 번 합체

.replace(/(<div[^>]*>|<\/div>)/g, "").replace(/(<span[^>]*>|<\/span>)/g, "").replace(/(<p[^>]*>|<\/p>)/g, "").replace(/ class="[^"]*"/g, "").replace(/ class="[^"]*"| class=[^ >]*/g, "").replace(/ style="[^"]*"/g, "").replace(/ colspan="[^"]*"/g, "").replace(/ rowspan="[^"]*"/g, "");

table 속성 추가

<div style="width: auto; overflow: auto;"> 
<table class="custom-table" style="white-space: nowrap; width: auto;">
  <thead>
  	<tr><th></th></tr>
  </thead>
  <tbody>
  	<tr><td></td></tr>
  </tbody>	
</div>

0개의 댓글