英文:
Regular expression in HTML5 input pattern with German Umlaute
问题
I try to allow only certain letters in the HTML input field including German Umlaute.
However, using:
<input pattern="[a-zA-Z0-9-#@.+_ äöüÄÖÜ]" type="text" value="">
Gives the error (in Chrome):
Pattern attribute value [a-zA-Z0-9-#@.+_ äöüÄÖÜ] is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-zA-Z0-9-#@.+_ äöüÄÖÜ]/: Invalid escape
How to include the Umlaute in the input pattern attribute?
Update:
It works now. Escape the special characters: pattern="[a-zA-Z0-9\.\-\+ äöüÄÖÜ]*"
英文:
I try to allow only certain letters in the HTML input field including German Umlaute.
However, using:
<input pattern="[a-zA-Z0-9-#@.+_ \ä\ö\ü\Ä\Ö\Ü]" type="text" value="">
or alternatively:
<input pattern="[a-zA-Z0-9-#@.+_ äöüÄÖÜ]" type="text" value="">
Gives the error (in Chrome):
> Pattern attribute value [a-zA-Z0-9-#@.+_ äöüÄÖÜ] is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-zA-Z0-9-#@.+_ äöüÄÖÜ]/: Invalid escape
How to include the Umlaute in the input pattern attribute?
<br>
<br>
Update:
It works now. Escape the special characters: pattern="[a-zA-Z0-9\.\-\+ äöüÄÖÜ]*"
答案1
得分: 1
- 你的正则表达式模式中有三个错误。
\ä
无法被浏览器像\b
和\s
一样转义,因为ä
不是一个特殊字符。-
被视为字符串而不是[from-to]
,它必须被转义为\\-
。- 需要转义的四个字符是:
// 需要转义 [ ] - \
- 需要在模式的末尾添加
*
以匹配多个字符。 - 在
9
后面的-
必须被转义。 - 当前版本的Chrome和FF中默认使用Unicode标志的正则表达式模式,浏览器会检查你的模式是否正确。
- 请参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
英文:
Three errors in your regExp pattern.
- ä can't be escape by Browser like \b \s, because of ä is not a special character.
- while - is considered as a string instead of [from-to], it must be escaped as \-.
// don't need escape
. _ = * ^ $ etc.
Four character need to be escaped are :
// need escape
[ ] - \ .
- need a * at the end of your pattern to match more than one character.
The - after 9 must be escaped.
<input pattern="[a-zA-Z0-9\-#@.+_ äöüÄÖÜ]*" type="text" value="">
Unicode flag used by default in pattern regExp in the current versions of Chrome and FF, and Browser will check your pattern is right or wrong.
See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论