-- JavaScript
정규식으로 특정 태그 안의 문자 추출 및 치환
어린왕자악꿍
2021. 1. 7. 15:26
일감 중에 <script>alert(1);</script> 와 같은 스크립트성 문자들을 없앨 일이 있어 검색 후 정리해둔다.
1. 태그 안의 문자 추출하기
var text = "hello <script>alert(1);</script> world";
text = text.match(/\<script>.*\<\/script>/gi);
text += "";
text = text.split("<script>").join("");
text = text.split("</script>").join("");
결과 : alert(1);
2. 태그 자체를 치환하기
var text = "hello <script>alert(1);</script> world";
text = text.replace(/\<script>.*\<\/script>/gi, '');
결과 : hello world