개발자의 길

[javascript] html 태그 삭제 본문

2. JS

[javascript] html 태그 삭제

자르르 2013. 1. 10. 11:55


 

 

//input - 글 내용

//allowed - 예외 태그

function strip_tags (input, allowed) {   
 allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join('');
 // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)   
 var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;   
 return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {       
  return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';    });
}

 

 



이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.
공유하기 링크
Comments