I don't know how to create a regular expression in JavaScript or jQuery.
我不知道如何用JavaScript或jQuery创建正则表达式。
I want to create a regular expression that will check if a string contains only characters between a-z and A-Z with any arrangement.
我想要创建一个正则表达式,该表达式将检查一个字符串是否只包含a-z和a-z之间的字符,并进行任何排列。
EDIT
编辑
When I tried to make regex
当我试着做regex的时候
/^[a-zA-Z\s]+$/
to accept white spaces as well. It is not working. What could be the mistake?
也接受空白。它不工作。会有什么错误呢?
I am testing my regular expression at JavaScript RegExp Example: Online Regular Expression Tester.
我正在JavaScript RegExp示例中测试我的正则表达式:在线正则表达式测试器。
4 个解决方案
#1
54
/^[a-zA-Z]*$/
Change the *
to +
if you don't want to allow empty matches.
如果您不想允许空匹配,则将*更改为+。
References:
Character classes ([...]
), Anchors (^
and $
), Repetition (+
, *
)
字符类([…]),锚(^和$),重复(+,*)
The /
are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
/只是分隔符,它表示regex的开始和结束。它的一个用途是现在你可以对它使用修饰符。
#2
23
Piggybacking on what the other answers say, since you don't know how to do them at all, here's an example of how you might do it in JavaScript:
参考其他答案,因为你根本不知道怎么做,这里有一个用JavaScript实现的例子:
var charactersOnly = "This contains only characters";
var nonCharacters = "This has _@#*($()*@#$(*@%^_(#@!$ non-characters";
if (charactersOnly.search(/[^a-zA-Z]+/) === -1) {
alert("Only characters");
}
if (nonCharacters.search(/[^a-zA-Z]+/)) {
alert("There are non characters.");
}
The /
starting and ending the regular expression signify that it's a regular expression. The search
function takes both strings and regexes, so the /
are necessary to specify a regex.
正则表达式的开始和结束表示它是正则表达式。搜索函数同时包含字符串和regex,因此需要指定一个regex。
From the MDN Docs, the function returns -1
if there is no match.
在MDN文档中,如果没有匹配,函数返回-1。
Also note: that this works for only a-z, A-Z. If there are spaces, it will fail.
注意:这只适用于a-z, a-z。如果有空间,它就会失败。
#3
8
/^[a-zA-Z]+$/
Off the top of my head.
在我的头顶上。
Edit:
编辑:
Or if you don't like the weird looking literal syntax you can do it like this
如果你不喜欢看起来很奇怪的字面句法你可以这样做
new RegExp("^[a-zA-Z]+$");
#4
3
/[:alpha:]+/
Any alpha character A to Z or a to z.
任何字符A到Z或A到Z。
or
或
/^[[:alpha:]]+$/s
to match strictly with spaces.
严格匹配空格。
#1
54
/^[a-zA-Z]*$/
Change the *
to +
if you don't want to allow empty matches.
如果您不想允许空匹配,则将*更改为+。
References:
Character classes ([...]
), Anchors (^
and $
), Repetition (+
, *
)
字符类([…]),锚(^和$),重复(+,*)
The /
are just delimiters, it denotes the start and the end of the regex. One use of this is now you can use modifiers on it.
/只是分隔符,它表示regex的开始和结束。它的一个用途是现在你可以对它使用修饰符。
#2
23
Piggybacking on what the other answers say, since you don't know how to do them at all, here's an example of how you might do it in JavaScript:
参考其他答案,因为你根本不知道怎么做,这里有一个用JavaScript实现的例子:
var charactersOnly = "This contains only characters";
var nonCharacters = "This has _@#*($()*@#$(*@%^_(#@!$ non-characters";
if (charactersOnly.search(/[^a-zA-Z]+/) === -1) {
alert("Only characters");
}
if (nonCharacters.search(/[^a-zA-Z]+/)) {
alert("There are non characters.");
}
The /
starting and ending the regular expression signify that it's a regular expression. The search
function takes both strings and regexes, so the /
are necessary to specify a regex.
正则表达式的开始和结束表示它是正则表达式。搜索函数同时包含字符串和regex,因此需要指定一个regex。
From the MDN Docs, the function returns -1
if there is no match.
在MDN文档中,如果没有匹配,函数返回-1。
Also note: that this works for only a-z, A-Z. If there are spaces, it will fail.
注意:这只适用于a-z, a-z。如果有空间,它就会失败。
#3
8
/^[a-zA-Z]+$/
Off the top of my head.
在我的头顶上。
Edit:
编辑:
Or if you don't like the weird looking literal syntax you can do it like this
如果你不喜欢看起来很奇怪的字面句法你可以这样做
new RegExp("^[a-zA-Z]+$");
#4
3
/[:alpha:]+/
Any alpha character A to Z or a to z.
任何字符A到Z或A到Z。
or
或
/^[[:alpha:]]+$/s
to match strictly with spaces.
严格匹配空格。