正则表达式在HTML5输入模式中与德语Umlaute一起使用

huangapple go评论60阅读模式
英文:

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-#@.+_ &#228;&#246;&#252;&#196;&#214;&#220;]" 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\.\-\+ &#228;&#246;&#252;&#196;&#214;&#220;]*"

英文:

I try to allow only certain letters in the HTML input field including German Umlaute.

However, using:

&lt;input pattern=&quot;[a-zA-Z0-9-#@.+_ \&#228;\&#246;\&#252;\&#196;\&#214;\&#220;]&quot; type=&quot;text&quot; value=&quot;&quot;&gt;

or alternatively:

&lt;input pattern=&quot;[a-zA-Z0-9-#@.+_ &#228;&#246;&#252;&#196;&#214;&#220;]&quot; type=&quot;text&quot; value=&quot;&quot;&gt;

Gives the error (in Chrome):

> Pattern attribute value [a-zA-Z0-9-#@.+_ &#228;&#246;&#252;&#196;&#214;&#220;] is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-zA-Z0-9-#@.+_ &#228;&#246;&#252;&#196;&#214;&#220;]/: Invalid escape

How to include the Umlaute in the input pattern attribute?

<br>
<br>

Update:

It works now. Escape the special characters: pattern=&quot;[a-zA-Z0-9\.\-\+ &#228;&#246;&#252;&#196;&#214;&#220;]*&quot;

答案1

得分: 1

  1. 你的正则表达式模式中有三个错误。
  2. \&#228; 无法被浏览器像 \b\s 一样转义,因为 &#228; 不是一个特殊字符。
  3. - 被视为字符串而不是 [from-to],它必须被转义为 \\-
  4. 需要转义的四个字符是:
    // 需要转义
    [ ] - \
    
  5. 需要在模式的末尾添加 * 以匹配多个字符。
  6. 9 后面的 - 必须被转义。
  7. 当前版本的Chrome和FF中默认使用Unicode标志的正则表达式模式,浏览器会检查你的模式是否正确。
  8. 请参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode
英文:

Three errors in your regExp pattern.

  1. &#228; can't be escape by Browser like \b \s, because of ä is not a special character.
  2. while - is considered as a string instead of [from-to], it must be escaped as \-.
// don&#39;t need escape
. _ = * ^ $    etc.

Four character need to be escaped are :

// need escape
[ ] - \ .
  1. need a * at the end of your pattern to match more than one character.

The - after 9 must be escaped.

&lt;input pattern=&quot;[a-zA-Z0-9\-#@.+_ &#228;&#246;&#252;&#196;&#214;&#220;]*&quot; type=&quot;text&quot; value=&quot;&quot;&gt;

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

huangapple
  • 本文由 发表于 2023年2月18日 16:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定