简体   繁体   English

HTML5中的正则表达式模式匹配无法正常工作

[英]regex pattern matching in HTML5 is not working properly

I have an input text box in my HTML form which looks for a regex pattern as shown below. 我的HTML表单中有一个输入文本框,用于寻找正则表达式模式,如下所示。 I am looking for anything to be entered other than white spaces or blank. 我正在寻找除空格或空格以外的任何要输入的内容。 I tried all the following below and none of them is allowing me to enter any normal text such as "hello world" and "helloworld" in it.Any suggestions are most welcome. 我在下面尝试了以下所有方法,但没有一个允许我在其中输入任何普通文本,例如“ hello world”和“ helloworld”。欢迎您提出任何建议。 Thanks 谢谢

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^\S$" title="Enter something valid"/>

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^[^\s]*$" title="Enter something valid"/>

<input name="item" type="text" size="25" autofocus="autofocus" pattern="^[\S]*$" title="Enter something valid"/>

EDIT: 编辑:

after removing the anchor, this works for "helloworld" but not for "hello world". 删除锚点后,此选项适用于“ helloworld”,但不适用于“ hello world”。 So I think it has to do with regex pattern. 因此,我认为这与正则表达式模式有关。

<input name="item" type="text" size="25" autofocus="autofocus" pattern="[^\s]*" title="Enter something valid"/>

[^\\s]* will match against anything that contains no spaces, so a space in the words will not match. [^\\s]*将与任何不包含空格的内容匹配,因此单词中的空格将不匹配。

You probably want something like .*[^\\s].* to match a string with at least one non-space character. 您可能希望使用.*[^\\s].*来匹配至少包含一个非空格字符的字符串。

The required attribute is probably the best way to guard against blanks (or ^$ should work). required属性可能是防止空格的最佳方法(或^$应该起作用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM