JS轉換HTML轉義符

//去掉html標籤html

1
2
3
function  removeHtmlTab(tab) {
  return  tab.replace(/<[^<>]+?>/g, '' ); //刪除全部HTML標籤
}

//普通字符轉換成轉意符spa

1
2
3
function  html2Escape(sHtml) {
  return  sHtml.replace(/[<>& "]/g,function(c){return {'<':'&lt;','>':'&gt;','&':'&amp;','" ': '&quot;' }[c];});
}

//轉意符換成普通字符code

1
2
3
4
function  escape2Html(str) {
  var  arrEntities={ 'lt' : '<' , 'gt' : '>' , 'nbsp' : ' ' , 'amp' : '&' , 'quot' : '"' };
  return  str.replace(/&(lt|gt|nbsp|amp|quot);/ig, function (all,t){ return  arrEntities[t];});
}

// &nbsp;轉成空格htm

1
2
3
4
function  nbsp2Space(str) {
  var  arrEntities = { 'nbsp'  : ' ' };
  return  str.replace(/&(nbsp);/ig, function (all, t){ return  arrEntities[t]})
}

//回車轉爲br標籤ci

1
2
3
function  return2Br(str) {
  return  str.replace(/\r?\n/g, "<br />" );
}

//去除開頭結尾換行,並將連續3次以上換行轉換成2次換行rem

1
2
3
4
5
6
function  trimBr(str) {
  str=str.replace(/((\s|&nbsp;)*\r?\n){ 3 ,}/g, "\r\n\r\n" ); //限制最多2次換行
  str=str.replace(/^((\s|&nbsp;)*\r?\n)+/g, '' ); //清除開頭換行
  str=str.replace(/((\s|&nbsp;)*\r?\n)+$/g, '' ); //清除結尾換行
  return  str;
}

// 將多個連續空格合併成一個空格string

1
2
3
4
function  mergeSpace(str) {
  str=str.replace(/(\s|&nbsp;)+/g, ' ' );
  return  str;
}
相關文章
相關標籤/搜索