admin管理员组

文章数量:1202596

I would like to know what's the best way to check a string for example (mail, password ..Etc).

/^...$/i.exec(a)

vs

/^...$/i.test(a)
  • exec returns value
  • test true

test:

// 1° way
var mail = req.body.mail;
if(check(mail)){

exec:

// 1° way
var mail = req.body.mail;
if(check(mail)){

// 2° way
var mail = check(req.body.mail);
if(mail){

exec or test ? and what number ( 1° or 2° if exec)

SOLUTION

test is better for this case.

  • it will surely be faster.

But the most important

  • test performs all his work. While exec did not perform, because more can be done, but we do not need.
  • Like said Mattias Buelens, using isMail() it's more logical: is an email: yes or no. While exec: is an email: email or null -> wtf ? lol

I would like to know what's the best way to check a string for example (mail, password ..Etc).

/^...$/i.exec(a)

vs

/^...$/i.test(a)
  • exec returns value
  • test true

test:

// 1° way
var mail = req.body.mail;
if(check(mail)){

exec:

// 1° way
var mail = req.body.mail;
if(check(mail)){

// 2° way
var mail = check(req.body.mail);
if(mail){

exec or test ? and what number ( 1° or 2° if exec)

SOLUTION

test is better for this case.

  • it will surely be faster.

But the most important

  • test performs all his work. While exec did not perform, because more can be done, but we do not need.
  • Like said Mattias Buelens, using isMail() it's more logical: is an email: yes or no. While exec: is an email: email or null -> wtf ? lol
Share Improve this question edited Mar 28, 2013 at 17:32 John Baber-Lucero 2,6523 gold badges18 silver badges20 bronze badges asked Jun 12, 2012 at 21:12 user1255808user1255808 5
  • What do you mean by 'best way'? – j08691 Commented Jun 12, 2012 at 21:14
  • 1st. Depends on what you're trying to do. Do you need matching/capturing groups and such or just validate to check if your provided string matches a regex? Also, the 2nd question is preference based, it'll have the same result. You should choose which is more readable and maintainable for you/your team. – Fabrício Matté Commented Jun 12, 2012 at 21:16
  • there are two solutions, there are differents, so i think one of them is best EDIT @Fabrício Matté: just validate. Yes i think thant the first way is best because is more readable ans logic. (for you ;) ) – user1255808 Commented Jun 12, 2012 at 21:16
  • 1 .test will return either a boolean true or false so it's optimal for that situation. Both would return values which evaluate to true/false, but if you don't need to store matches or capturing groups then .test will do. =] – Fabrício Matté Commented Jun 12, 2012 at 21:19
  • Ok, thank you man. Yes did not want to talk about micro optimization. But in this case.. Both have the same level of readability. But test is best for this way. More logical, and faster ! EDIT As you're here ;) i would like to know if i'm obliged to check if the variable isn't undefined same time as regex. Or you have a better technique ? thks – user1255808 Commented Jun 12, 2012 at 21:26
Add a comment  | 

1 Answer 1

Reset to default 23

If you only need to test an input string to match a regular expression, RegExp.test is most appropriate. It will give you a boolean return value which makes it ideal for conditions.

RegExp.exec gives you an array-like return value with all capture groups and matched indexes. Therefore, it is useful when you need to work with the captured groups or indexes after the match. (Also, it behaves a bit different compared to String.match when using the global modifier /g)

Ultimately, it won't matter much in speed or efficiency. The regular expression will still be evaluated and all matching groups and indexes will be available through the global RegExp object (although it's highly recommended that you use the return values).

As for the if test, that's just a matter of personal taste. Assigning the result of the regular expression test to a variable with a meaningful name (such as isEmail) could improve the readability, but other than that they're both fine.

本文标签: JavaScript test vs execStack Overflow