今天和朋友谈到对前端工程师加强正则训练的事情。于是分享三个在html文本过滤的时候最常用到的函数,这些函数都采用正则进行处理。
/* * 去掉HTML标签 */function stripHTML(oldString) { return oldString.replace(/<\/?[^>]+>/gi, "");}/* * 去掉<br> */function stripBR(oldString) { return oldString.replace(/<br.{0,}?>/gi, "");}/* * 去掉全角空格和半角空格 */function stripSpace(string) { var tempstr; tempstr = string.replace(/(^\s+)|(\s+$)/g, ""); tempstr = tempstr.replace(/(^ +)|( +$)/g, ""); return tempstr;}
HTML怎么学习?HTML怎么入门?HTML在哪学?HTML怎么学才快?不用担心,这里为大家提供了HTML速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号