js去除字符串中所有html标签及&nbsp符号

时间:2016-04-15 23:05:55   收藏:0   阅读:3093

近日在做项目的时候,经常会在页面上处理一些数据。结果发现自己js掌握的并不是很好。那就在这里记录js的点点滴滴吧。

 

1. 去除字符串中的 html 标签

function delHtmlTag(str){
  return str.replace(/<[^>]+>/g,"");
}
var str = "<span style=‘display:none;‘>This is test</span><br/>";
str = delHtmlTag(str);
alert(str);

2. 去除字符串中的 &nbsp 空格符号

var test = "this &nbsp; is &nbsp; a test";
test = test.replace(/&nbsp;/ig, "");
alert(test);

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!