Ⅰ 怎樣用js方法過濾html等代碼
^<input type="text" id="theOne" value="">
<input type="button" onclick="NoHtml()" value="過濾html標簽">
<script>
function NoHtml(){
var t=document.getElementById("theOne").value;
t=t.replace(/({|})/g,''); //過濾{}
t=t.replace(/</g,'<'); //置換符號<
t=t.replace(/>/g,'>'); //置換符號>
// t=t.replace(/<\/?[^>]*>/g,''); //*<\/?[^>]*>可以匹配<script></style></body>等,並置空。而不是替內換容<和>兩個符號
document.getElementById("theOne").value=t;
}
</script>
Ⅱ js 如何過濾div里內的指定字元
String.replace(正則表達式,"")
replace是string類型內置的替換方法,第一個參數可以是正則表達式,第二個參數是版想要權替換成的文本,正則中可以使用/g來表示替換所有匹配的文本,不使用則代表只替換匹配到的第一個字元對象,將第二個參數設為空字元串便可達到過濾的效果。
具體正則需要你自己去了解關於正則的知識了,祝你好運。
Ⅲ js 過濾所有空字元串
首先要理解這個方法replace。
語法:string.replace(searchvalue,newvalue)
searchvalue:必須。規定子字元串或要替換的版模式的 RegExp 對象。
請注意權,如果該值是一個字元串,則將它作為要檢索的直接量文本模式,而不是首先被轉換為 RegExp 對象。
newvalue:必需。一個字元串值。規定了替換文本或生成替換文本的函數。
注意RegExp 對象,而// 就是一個RegExp 對象(正則表達式)。
//varRegExp=newRegExp(pattern,attributes);
varreg=newRegExp('','g');
str=str.replace(reg,'');//跟str=str.replace(//g,'')是一樣意思。
而這里的g是global的縮寫,意思是全局匹配;
如果沒有加g,那麼只是匹配第一個就結束了,對應str2,否則就全局匹配,對應str1
Ⅳ js 正則過濾特殊字元
您好
js檢查是否含有非法字元,js 正則過濾特殊字元
//正則
functiontrimTxt(txt){
returntxt.replace(/(^s*)|(s*$)/g,"");
}
/**
*檢查是否含有非法字元
*@paramtemp_str
*@returns{Boolean}
*/
functionis_forbid(temp_str){
temp_str=trimTxt(temp_str);
temp_str=temp_str.replace('*',"@");
temp_str=temp_str.replace('--',"@");
temp_str=temp_str.replace('/',"@");
temp_str=temp_str.replace('+',"@");
temp_str=temp_str.replace(''',"@");
temp_str=temp_str.replace('\',"@");
temp_str=temp_str.replace('$',"@");
temp_str=temp_str.replace('^',"@");
temp_str=temp_str.replace('.',"@");
temp_str=temp_str.replace(';',"@");
temp_str=temp_str.replace('<',"@");
temp_str=temp_str.replace('>',"@");
temp_str=temp_str.replace('"',"@");
temp_str=temp_str.replace('=',"@");
temp_str=temp_str.replace('{',"@");
temp_str=temp_str.replace('}',"@");
varforbid_str=newString('@,%,~,&');
varforbid_array=newArray();
forbid_array=forbid_str.split(',');
for(i=0;i<forbid_array.length;i++){
if(temp_str.search(newRegExp(forbid_array[i]))!=-1)
returnfalse;
}
returntrue;
}
---------------------
作者:dongsir 董先生
來源:董先生的博客
原文鏈接:js檢查是否含有非法字元
版權聲明:本作品採用知識共享署名-非商業性使用-相同方式共享 4.0 國際許可協議進行許可。轉載時請標註:http://dongsir.cn/p/195
Ⅳ js中filter過濾用法總結
filter() 方法創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。
注意: filter() 不會對空數組進行檢納賣測。
注意: filter() 不會改變原始數組。
| 參數 | 描述 |
| function(currentValue, index,arr) | 必須。函數,數組中的每個元素都會執行這個函數
函數參數:
| 參數 | 描述 |
| currentValue | 必須。當前元素的值 |
| index | 可選。當期元素的索引值 |
| arr | 可選。當期元素屬於的數組對象 |
|
| thisValue | 可選。對象作為該執行回調時使用,傳遞給函數,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值為 "undefined" |
首先回顧一下filter的作用:過濾數組中符合條件的元素
另外也可以用來過濾對象數組中老衡符合條件的對象,eg:
|
目前比較常用的方法是使用ES6的set完成,eg:
結合map使用可以先過濾出符合條件的對象然後去除某些不需洞含逗要的欄位,比如:
簡單講filter就是一個數組過濾器,參數接收一個函數,數組的每一項經過函數過濾,返回一個符合過濾條件的新數組
函數接收三個參數:
Ⅵ JS怎麼用關鍵字來過濾表格
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>RunJS演示代碼</title>
<script>
=function(){
vartrs=tab1.tBodies[0].rows;
for(vari=0;i<trs.length;i++){
vartr=trs[i];
if(tr.innerHTML.search(oTXT.value)==-1)
tr.style.display='none';
else
tr.style.display='';
}
}
</script>
</head>
<body>
<form>
<span>輸入查詢的關鍵字:</span>
<inputtype="text"id="oTXT"/>
<inputtype="button"value="查找"onclick="chazhao()"/>
</form>
<tableborder="1px"id="tab1"width="500">
<thead>
<tr>
<td>姓名</td>
<td>職位</td>
<td>手機</td>
<td>分機</td>
</tr>
</thead>
<tbody>
<tr>
<td>111</td>
<td>222</td>
<td>333</td>
<td>444</td>
</tr>
<tr>
<td>589</td>
<td>666</td>
<td>777</td>
<td>888</td>
</tr>
<tr>
<td>567</td>
<td>888</td>
<td>999</td>
<td>999</td>
</tr>
</tbody>
</table>
</body>
</html>
Ⅶ 如何用js或則jquery過濾特殊字元
1、jQuery使用正則匹配替換特殊字元
functionRegeMatch(){
varpattern=newRegExp("[~'!@#$%^&*()-+_=:]");
if($("#name").val()!=""&&$("#name").val()!=null){
if(pattern.test($("#name").val())){
alert("非法字元!");
$("#name").attr("value","");
$("#name").focus();
returnfalse;
}
}
}
2、jQuery限制輸入ASCII值
//數字0-9的ascii為48-57
//大寫A-Z的ascii為65-90
//小寫a-z的ascii為97-122
//----------------------------------------------------------------------
//<summary>
//限制只能輸入數字和字母
//</summary>
//----------------------------------------------------------------------
$.fn.onlyNumAlpha=function(){
$(this).keypress(function(event){
vareventObj=event||e;
varkeyCode=eventObj.keyCode||eventObj.which;
if((keyCode>=48&&keyCode<=57)||(keyCode>=65&&keyCode<=90)||(keyCode>=97&&keyCode<=122))
returntrue;
else
returnfalse;
}).focus(function(){
this.style.imeMode='disabled';
}).bind("paste",function(){
varclipboard=window.clipboardData.getData("Text");
if(/^(d|[a-zA-Z])+$/.test(clipboard))
returntrue;
else
returnfalse;
});
};
//-----調用方法$("#文本框id").onlyNumAlpha();
3、js正則匹配過濾
functionstripscript(s)
{
varpattern=newRegExp("[`~!@#$^&*()=|{}':;',\[\].<>/?~!@#¥……&*()——|{}【】『;:」「'。,、?]")
varrs="";
for(vari=0;i<s.length;i++){
rs=rs+s.substr(i,1).replace(pattern,'');
}
returnrs;
}
Ⅷ js過濾數組中都為空的對象幾種方式
1、filter() 方法創建一個新的數組,新數組中的元素是通過檢查指定數組中符合條件的所有元素。
原數組不變
不會對空數組進行檢測
2、find() 對於空數組,函數是不會執行的。
不會改變原數組
返回符合測試條件的第一個數組元素值
3、some 用於檢測數組中的元素是否滿足指定條件
會依次執行數組的每個元素-如果有一個元素滿足條件(即只要有條件滿足即可相當於或),則表達式返回true , 剩餘的元素不會再執行檢測
----------------------如果沒有滿足條件的元素,則返回false
4、every() 方法用於檢測數組所有元素是否都符合指定條件(通過函數提供)
every() 方法使用指定函數檢測數組中的所有元素-如果數組中檢測到有一個元素不滿足,則整個表達式返回 false ,且剩餘的元素不會再進行檢測
------------------------------------------如果所有元素都滿足條件,則返回 true
Ⅸ JS正則過濾指定的HTML標簽
1,得到網頁上的鏈接源地址:
string
matchString =
@"<a[^>]+href=\s*(?:'(?<href>[^']+)'|""(?<href>[^""]+)""|(?<href>[^>\s]+))\s*[^>]*>";
2,得到網頁的標題:
string matchString = @"<title>(?<title>.*)</title>";
3,去掉網頁中的所有的html標記:
string temp = Regex.Replace(html, "<[^>]*>", ""); //html是一個要去除html標記的文檔
4, string matchString = @"<title>([\S\s\t]*?)</title>";
5,js去掉所有html標記的函數:
function delHtmlTag(str)
{
return str.replace(/<[^>]+>/g,"");//去掉所有的html標記
}