我有下面的代碼檢查「ENTER」鍵,以及防止在文本框中使用>
和<
標誌。如何更換代碼中使用正則表達式
<script type="text/javascript" language="javascript">
function checkKeycode(e) {
var keycode;
if (window.event) // IE
keycode = e.keyCode;
else if (e.which) // Netscape/Firefox/Opera
keycode = e.which;
if (keycode == 13) {
//Get the button the user wants to have clicked
var btn = document.getElementById(btnSearch);
if (btn != null) { //If we find the button click it
btn.click();
event.keyCode = 0
}
//Removed when above code was added 12-09-13
//CallSearch();
}
}
function CallSearch() {
var objsearchText = window.document.getElementById('txtSearchText');
var searchText;
if ((objsearchText!=null))
{
searchText = objsearchText.value;
searchText = searchText.replace(/>/gi, " >");
searchText = searchText.replace(/</gi, "< ");
objsearchText.value = searchText;
}
//This cookie is used for the backbutton to work in search on postback
//This cookie must be cleared to prevent old search results from displayed
document.cookie='postbackcookie=';
document.location.href="search_results.aspx?searchtext=";
}
</script>
我怎樣才能縮短代碼更effecient並使用onBlur
功能和使用RegExp
,而不是取代?或者取代比RegExp
更快的方法?
您在替換中使用正則表達式。 '/> /'是一個正則表達式。 –
你想使用'onBlur'函數來做什麼?另外,您在正則表達式中使用的[i修飾符](http://www.w3schools.com/jsref/jsref_regexp_i.asp)是不必要的,因爲您並不試圖匹配字母。 – NathanW
我不能將兩者合併爲一行嗎?我想使用'onBlur()',這樣當用戶離開文本框並將Lt和gt替換爲'' – Si8