I want to use JavaScript (can be with jQuery) to do some client-side validation to check whether a string matches the regex:
我想使用JavaScript(可以使用jQuery)进行一些客户端验证,以检查字符串是否与regex匹配:
^([a-z0-9]{5,})$
Ideally it would be an expression that returned true or false.
理想情况下,它是返回true或false的表达式。
I'm a JavaScript newbie, does match()
do what I need? It seems to check whether part of a string matches a regex, not the whole thing.
我是一个JavaScript新手,匹配()做我需要的吗?它似乎检查字符串的一部分是否匹配regex,而不是整个。
6 个解决方案
#1
764
Use regex.test()
if all you want is a boolean result:
如果您想要的只是一个布尔结果,则使用regex.test():
/^([a-z0-9]{5,})$/.test('abc1'); // false
/^([a-z0-9]{5,})$/.test('abc12'); // true
/^([a-z0-9]{5,})$/.test('abc123'); // true
...and you could remove the ()
from your regexp since you've no need for a capture.
…您可以从regexp中删除(),因为您不需要捕获。
#2
103
Use test()
method :
使用测试()方法:
var term = "sample1";
var re = new RegExp("^([a-z0-9]{5,})$");
if (re.test(term)) {
console.log("Valid");
} else {
console.log("Invalid");
}
#3
47
You can use match()
as well:
您也可以使用match():
if (str.match(/^([a-z0-9]{5,})$/)) {
alert("match!");
}
But test()
seems to be faster as you can read here.
但是test()似乎更快,您可以在这里阅读。
Important difference between match()
and test()
:
match()与test()的重要区别:
match()
works only with strings, but test()
works also with integers.
match()只适用于字符串,而test()也适用于整数。
12345.match(/^([a-z0-9]{5,})$/); // ERROR
/^([a-z0-9]{5,})$/.test(12345); // true
/^([a-z0-9]{5,})$/.test(null); // false
// Better watch out for undefined values
/^([a-z0-9]{5,})$/.test(undefined); // true
#4
33
Use /youregexp/.test(yourString)
if you only want to know whether your string matches the regexp.
如果您只想知道您的字符串是否与regexp匹配,请使用/youregexp/.test(您的字符串)。
#5
6
Here's an example that looks for certain HTML tags so it's clear that /someregex/.test()
returns a boolean:
这里有一个寻找特定HTML标记的示例,因此很明显/someregex/.test()返回一个布尔值:
if(/(span|h[0-6]|li|a)/i.test("h3")) alert('true');
#6
1
let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let regexp = /[a-d]/gi;
console.log(str.match(regexp));
#1
764
Use regex.test()
if all you want is a boolean result:
如果您想要的只是一个布尔结果,则使用regex.test():
/^([a-z0-9]{5,})$/.test('abc1'); // false
/^([a-z0-9]{5,})$/.test('abc12'); // true
/^([a-z0-9]{5,})$/.test('abc123'); // true
...and you could remove the ()
from your regexp since you've no need for a capture.
…您可以从regexp中删除(),因为您不需要捕获。
#2
103
Use test()
method :
使用测试()方法:
var term = "sample1";
var re = new RegExp("^([a-z0-9]{5,})$");
if (re.test(term)) {
console.log("Valid");
} else {
console.log("Invalid");
}
#3
47
You can use match()
as well:
您也可以使用match():
if (str.match(/^([a-z0-9]{5,})$/)) {
alert("match!");
}
But test()
seems to be faster as you can read here.
但是test()似乎更快,您可以在这里阅读。
Important difference between match()
and test()
:
match()与test()的重要区别:
match()
works only with strings, but test()
works also with integers.
match()只适用于字符串,而test()也适用于整数。
12345.match(/^([a-z0-9]{5,})$/); // ERROR
/^([a-z0-9]{5,})$/.test(12345); // true
/^([a-z0-9]{5,})$/.test(null); // false
// Better watch out for undefined values
/^([a-z0-9]{5,})$/.test(undefined); // true
#4
33
Use /youregexp/.test(yourString)
if you only want to know whether your string matches the regexp.
如果您只想知道您的字符串是否与regexp匹配,请使用/youregexp/.test(您的字符串)。
#5
6
Here's an example that looks for certain HTML tags so it's clear that /someregex/.test()
returns a boolean:
这里有一个寻找特定HTML标记的示例,因此很明显/someregex/.test()返回一个布尔值:
if(/(span|h[0-6]|li|a)/i.test("h3")) alert('true');
#6
1
let str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let regexp = /[a-d]/gi;
console.log(str.match(regexp));