A. js字元串的正則匹配 如何過濾掉指定特徵的字元串
String.replace(正則表達式,"")
replace是string類型內置的替換方法,第一個參數可以是版正則表達式,第二個參數是想要替換成的文本權,正則中可以使用/g來表示替換所有匹配的文本,不使用則代表只替換匹配到的第一個字元對象,將第二個參數設為空字元串便可達到過濾的效果。
具體正則需要你自己去了解關於正則的知識了,祝你好運。
B. 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
C. 如何用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
//大寫-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;
}
D. 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
E. java過濾sql關鍵字的正則替換掉
java過濾sql關鍵字的正則替換掉方法如下:
可以在C#中這樣做:Regexregex = newRegex(@"]*>[^");
stringcleanedHtml = regex.Replace(html, "");
可是我並不想再寫個循環去遍歷每條記錄,然後保存每條記錄,我想在資料庫中一步到位,而sql只提供了簡單的replace函數,這個函數明顯不能達到咱的要求,那就去寫一個自定義函數吧。
函數源代碼如下:CREATE functiondbo.regexReplace
(@source ntext,--原字元串@regexp varchar(1000),--正則表達式@replace varchar(1000),--替換值@globalReplace bit=1,--是否是全局替換@ignoreCase bit=0 --是否忽略大小寫)returnS varchar(1000)AS
begin
declare@hr intege
declare@objRegExp integer
declare@result varchar(5000)exec@hr =sp_OACreate'VBScript.RegExp',@objRegExp OUTPUT
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Pattern',@regexp
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'Global',@globalReplace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OASetProperty@objRegExp,'IgnoreCase',@ignoreCase
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OAMethod@objRegExp,'Replace',@result OUTPUT,@source,@replace
IF@hr <>0 begin
exec@hr =sp_OADestroy@objRegExp
returnnullend
exec@hr =sp_OADestroy@objRegExp
IF@hr <>0 begin
returnnullend
return@result
end
需要注意的是,即使寫好了這個函數,也並不能馬上使用。執行這個函數時可能會出現以下的錯誤:Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to procere 'sys.sp_OACreate' of component 'Ole Automation Proceres' because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of 'Ole Automation Proceres' by using sp_configure. For more information about enabling 'Ole Automation Proceres', see "Surface Area Configuration" in SQL Server Books Online.
這是因為未開啟Ole Automation Proceres選項,MSDN中的Ole Automation Proceres選項。執行下面的語句開啟這個選項:sp_configure'show advanced options',1;GO
RECONFIGURE;GOsp_configure'Ole Automation Proceres',1;GO
RECONFIGURE;GO
所有的准備工作都已經做好,那就試驗一下吧。
Example1:忽略大小寫並替換selectdbo.regexReplace(',']*>[^','',1,1)
Example2: 使用貪婪匹配
html代碼:
Also Available - Smith & Hogan: Criminal Law Cases & Materials 10th ed
There is, as ever, detailed analysis of the many recent case developments, in particular,
a revision of the chapter dealing with secondary liability and joint enterprise.
調用代碼:selectdbo.regexReplace(html,']*>(.|
)*?','',1,1)
Example3:去除html標簽selectdbo.regexReplace('
Key Contact:
Mr Jack, Zhou
General Manager
Mr A, Ho
Marketing Director
Overseas Sales
MsWinny, Luo
Sales Manager
Overseas Sales',']*>','',1,0)
Example4:資料庫欄位值替換updateBooks。
F. JS如何去除指定字元串
可以用replace函數去除指定字元串。
1、在body標簽和html標簽中添加一個script標簽,定義一個字元串,這回里以「這是個什麼答演示文本」為例,將此時的字元串輸出到頁面: