去掉html標籤
function delHtmlTag(str){
return str.replace(/<[^>]+>/g,"");//去掉所有的html標記
}
處理包含title的html標籤
function delHtmlTag(str, obj){
var title = str.replace(/<[^>]+>/g,"");//去掉所有的html標記
if(title.length > 300) {
title = title.substring(0,300);
}
obj.title = title;
}
去掉字符串前後所有空格
function Trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, "");
}
說明:如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前後所有空格的字符串。
去掉字符串中所有空格(包括中間空格,需要設置第2個參數爲:g)
function Trim(str, is_global) {
var result;
result = str.replace(/(^\s+)|(\s+$)/g, "");
if (is_global.toLowerCase() == "g") {
result = result.replace(/\s/g, "");
}
return result;
}
js 的replace方法將字符串中的 全部替換爲空字符串
var test="this is a test";
alert(test.replace(/ /ig, ""));