正则表达式——只检查非字母字符

时间:2022-06-07 02:36:31

I've started to study on javascript and regexp specially. I am trying to improve myself on generating regular expressions.

我已经开始专门研究javascript和regexp。我正在努力提高自己生成正则表达式的能力。

What I am trying to do is - I have a name field in my html file (its in a form ofc)

我要做的是——我的html文件中有一个name字段(它的形式是c)

...
    <label for="txtName">Your Name*: </label>
    <input id="txtName" type="text" name="txtName" size="30" maxlength="40">
...

And in my js file I am trying to check name cannot start with any NON-letter character, only for the first character. And whole field cannoth contain any special character other than dash and space.

在我的js文件中,我要检查名称不能以任何非字母字符开头,只能以第一个字符开头。整个场不能包含除虚线和空间之外的任何特殊性质。

They cannot start with any non-letter character ;

他们不能以任何非字母的字符开头;

/^[A-Z a-z][A-Z a-z 0-9]*$/

They cannot contain any symbol other than dash and space ;

它们不能包含任何符号,除了dash和space;

/^*[[&-._].*]{1,}$/

When I am testing if it works, it doesn't work at all. In which part am I failing ?

当我测试它是否有效时,它根本就不管用。在哪些方面我失败了?

1 个解决方案

#1


7  

Try /^[A-Za-z][A-Za-z0-9 -]*$/
When you include a space in the character listing ([]), it's going to be allowed in the string that's being searched.

Try / ^[A-Za-z][A-Za-z0-9 -]*美元/当你包括空间特征清单([])中,它将被允许在被搜索的字符串。

^[A-Za-z] matches the first character and says that it must be a letter of some sort.
[A-Za-z0-9 -]*$ will match any remaining characters(if they exist) after the first character and make sure it's alpha numeric, or contains spaces or dashes.

^[A-Za-z]匹配的第一个字符和说,这一定是某种的来信。[A-Za-z0-9 -]*$将匹配第一个字符后的任何剩余字符(如果它们存在),并确保它是alpha数值,或包含空格或破折号。

#1


7  

Try /^[A-Za-z][A-Za-z0-9 -]*$/
When you include a space in the character listing ([]), it's going to be allowed in the string that's being searched.

Try / ^[A-Za-z][A-Za-z0-9 -]*美元/当你包括空间特征清单([])中,它将被允许在被搜索的字符串。

^[A-Za-z] matches the first character and says that it must be a letter of some sort.
[A-Za-z0-9 -]*$ will match any remaining characters(if they exist) after the first character and make sure it's alpha numeric, or contains spaces or dashes.

^[A-Za-z]匹配的第一个字符和说,这一定是某种的来信。[A-Za-z0-9 -]*$将匹配第一个字符后的任何剩余字符(如果它们存在),并确保它是alpha数值,或包含空格或破折号。