❶ 怎麼使用js過濾html標簽
你可以利用正則表達式來剔除這些標簽,也就是將所有的html類的標簽都替換為空即可:
//去除HTML標簽
str=str.replace(/</?[^>]*>/g,'');
❷ 怎樣用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>
❸ 如何監聽驗證一個項目中的所有 input輸入框 自動過濾 html標簽 css樣式 和js腳本之類的
<input onkeyup="usrNameSet(this)" />
其它的自己可以隨便調用
Js部分
//只能輸入數字、字母、小數點、漢字、@
function usrNameSet(num){
var str=num.value;
//var str = document.getElementById("userName").value;
var value=str.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.]/g,'');
document.getElementById("userName").value=value;
}
//手機號碼、手機驗證碼,只能輸入數字
function numberSet(num){
var str=num.value;
var value=str.replace(/[^0-9]/g,'');
num.value=value;
}
//推廣碼驗證,只能輸入數字跟字母
function spreadCodeSet(num){
var str=num.value;
var value=str.replace(/[^\w\.\/]/g,'');
num.value=value;
}
//密碼設置 不能為空格
function passwordSet(num){
/*
var str=document.getElementById("password").value;
var value=str.replace(/^ +| +$/g,'');
document.getElementById("password").value=value;
*/
var str=num.value;
var value=str.replace(/^ +| +$/g,'');
num.value=value;
}
//登陸驗證碼
//只能是4位字母或數字
function UserVerifycodeSet(){
var str=document.getElementById("verifycode").value;
var value=str.replace(/[^a-zA-Z0-9]/g,'');
document.getElementById("verifycode").value=value;
}
//姓名和身份證認證
function nameNumberSet(num){
if(num.id=='text_name')//姓名 只能輸入漢字
{
var str=num.value;
var value=str.replace(/[^\u4E00-\u9FA5]/g,'');
num.value=value;
}
else if(num.id=='text_idcard'){//身份證 只能是數字和字母
var str=num.value;
var value=str.replace(/[^a-zA-Z0-9]/g,'');
num.value=value;
}
}
//郵箱輸入 //只能輸入數字、字母、小數點、漢字、@、-
function myMailSet(num){
var str=num.value;
var value = str.replace(/[^\a-\z\A-\Z0-9\u4E00-\u9FA5\@\.\-\_]/g,'');
num.value=value;
}
//充值輸入設置,小數點後只能兩位
function moneyInput(num){
//var str=num.value;
//alert(str);
//var value=str.replace(function(){
//if(this.value==this.value2)return;if(this.value.search(/^\d*(?:\.\d{0,2})?$/)==-1)this.value=(this.value2)?this.value2:'';else
this.value2=this.value;
//})
//var value=str.toFixed(2);
// var value = str.replace(/[^a-zA-Z0-9]/g,'');
// num.value=value;
if(num.value==num.value2)return;
if(num.value.search(/^\d*(?:\.\d{0,2})?$/)==-1)
num.value=(num.value2)?num.value2:'';
else num.value2=num.value;
}
//地址輸入設置
//只能輸入數字、字母、
function addressSet(num){
var str=num.value;
var value=str.replace(/[^\u4e00-\u9fa5\w]/g,'');
num.value=value;
}
//銀行卡輸入設置
function formatBankNoSet(BankNo){
//alert(BankNo.value);
var str=BankNo.value;
var value=str.replace(/[^0-9]/g,'');
BankNo.value=value;
}
//只能輸入字母、漢字
function cnOrEn(num){
var str=num.value;
var value=str.replace(/[^\a-zA-Z\u4E00-\u9FA5]/g,'');
num.value=value;
}
❹ 如何使用js正則 過濾某一個html標簽下所有的標簽跟樣式呢只保留出純文本
js過濾標簽的方法。分享給大家供大家參考,具體如下:
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>無標題文檔</title>
<script>
window.onload=function()
{
varoTxt1=document.getElementById('txt1');
varoTxt2=document.getElementById('txt2');
varoBtn=document.getElementById('btn');
oBtn.onclick=function()
{
varreg=/<[^<>]+>/g;
oTxt2.value=oTxt1.value.replace(reg,'');
};
};
</script>
</head>
<body>
<textareaid="txt1"cols="40"rows="10"></textarea><br/>
<inputtype="button"value="過濾"id="btn"/><br/>
<textareaid="txt2"cols="40"rows="10"></textarea>
</body>
</html>
❺ 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標記
}
❻ 在js中一個存儲html文本的對象,怎樣過濾其中的所有的img標簽
var reTag = /<img(?:.|\s)*?>/g;
var str = '<div><img id="img1" src="images/picture1.png" onclick="change()">234</div>'
alert(str.replace(reTag,''));