2013-12-20 25 views
1

box1中的正則表達式起作用,然後我在box2中添加正則表達式,並將其轉換爲我的用法 - 它看起來前方,不會讓用戶輸入前導空格。當我替換括號中的字符時,我得到了警告/錯誤(在日食中) - 我嘗試過轉義第一個字符 ,但那不能解決它 - 我有「逃脫」問題?謝謝您的幫助!HTML輸入字段項目預讀REGEX Javascript

if (v.match(/^\d{2}$/) !== null) works 

try  (v.match(^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$) !== null) fails 
     (v.match(/^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$) !== null) fails 

     errors undefined attribute name null 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
</head> 
<body> 
Typical InputBox1 
<input type="text" name="date" onkeyup="var v = this.value; 
if (v.match(/^\d{2}$/) !== null) {this.value = v + '/';} 
else if (v.match(/^\d{2}\/\d{2}$/) !== null) {this.value = v + '/';}" 
maxlength="10"> 
<br> 
Typical InputBox2 
<input type="text" name="date" onkeyup="var v = this.value; 
if (v.match(^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$) !== null) {this.value = v + '/';} 
else if (v.match(^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$) !== null) {this.value = v + '/';}" 
maxlength="10"> 

<!-- ^(?!\s)([a-zA-Z0-9 _.'"()!?&@]){1,}$ lookahead for leading blank space --> 

</body> 
</html> 

回答

0

@ M42你點上,但我發現轉義字符沒有在這裏工作 - 似乎要的是什麼是「HTML逃逸」不存在,但代替該報價焦炭&quot;&#34;至少將日食警告關閉。感謝您發現char問題。

0

轉義正則表達式中的所有雙引號。

(v.match(^(?!\s)([a-zA-Z0-9 _.'\"()!?&@]){1,}$) !== null) 
//      here __^ 
+0

我在網上測試了正則表達式,它似乎工作正常 - 你的建議並沒有解決它 - 似乎有很多逃避要做 - 我仔細嘗試,但仍然錯誤 – Tom

+0

需要轉義\。 + *? [^] $(){} =! < > | : - – Tom

+0

else if(v.match(\^\(\?\!\\ s \)\(\ [a \ -zA \ -Z0 \ -9 _ \。'「\(\)\!\? &#\] \)\ {1,\} \ $)!== null){this.value = v +'/';}「 maxlength =」10「> – Tom