4

I am trying to test the following for regex but am unable to get it to work. I am using regex101 but can anyone advise on how to correct this or format the regex?

I have an array - call it arr[] with 5 elements.

  • arr[0] needs to be a variable number of letters, i.e. a one-word name.
  • arr[1] needs to be either a one-word or two-word name (i.e. a variable number of letters or a variable number of letters followed by a whitespace character followed by another variable number of letters)
  • arr[2] needs to be 10 digits in a row (e.g. 123432908623, can start with 0 also)
  • arr[3] needs to be a social security number with the following format - 3 digits followed by whitespace followed by 2 digits followed by 4 digits (e.g. 123 45 6848)
  • arr[4] needs to be a phone number with area code with the following format - open parenthesis followed by 3 digits followed by closed parenthesis followed by whitespace followed by open parenthesis followed by 3 digits followed by closed parenthesis followed by whitespace followed by open parenthesis followed by 4 digits followed by close parenthesis (e.g. (123) 456 1234)

Here is some code for what I started with:

for(var i = 0; i<arr.length; i++){

    var str1 = arr[i][0];
    var pat1 = /^\s'w+?'/;
    var first = pat1.test(str1);
    console.log(first);

    var str2 = arr[i][1];
    var pat2 = /\s'\w+(?:\s\w+)?'/;
    var second = pat2.test(str2);
    console.log(second);

    var str3 = arr[i][2];
    var pat3 = /?:\d{10}$/;
    var third = pat3.test(str3);
    console.log(third);

    var str4 = arr[i][3];
    var pat4 = /?:\d{3}\s){2}\d{4}'\s\]$/;
    var fourth = pat4.test(str4);
    console.log(fourth);

    var str5 = arr[i][4];
    var pat5 = /\s'(?:\d{3}\s){3}\d{4})'\s\/;
    var fifth = pat5.test(str5);
    console.log(fifth);
}
3
  • Your question is?? also, please format your code in order to get help. Commented Apr 27, 2015 at 16:37
  • The question is the code I provided was not correct, but I wasn't sure how to move forward with correcting it. Commented Apr 27, 2015 at 16:45
  • Understood, I've tested all fields and it works perfectly, also, I've removed the array loop that wasn't making too much sense. Commented Apr 27, 2015 at 17:03

3 Answers 3

2

You appear to have a significant amount of typos in your code so far. Also, this looks like a problem set question so I will leave it up to you as a further exercise to understand what the corrections are. You were on the right track though:

var pat1 = /^[a-z]+$/i;
var pat2 = /^[a-z]+(?: [a-z]+)?$/i;
var pat3 = /^\d{10}$/;
var pat4 = /^\d{3} \d{2} \d{4}$/;
var pat5 = /^\(\d{3}\) \d{3} \d{4}$/;

for (var i = 0; i < arr.length; i++) {
  console.log(pat1.test(arr[i][0]));
  console.log(pat2.test(arr[i][1]));
  console.log(pat3.test(arr[i][2]));
  console.log(pat4.test(arr[i][3]));
  console.log(pat5.test(arr[i][4]));
}
Sign up to request clarification or add additional context in comments.

1 Comment

That's another question but I would point you at using something like filter: method creates a new array with all elements that pass the test implemented by the provided function.
1

I believe that using regex in the global format is more interesting, did a fiddle with easy maintenance ...

http://jsfiddle.net/579j2po1/1

var pat1 = /^[a-z]+$/i;
var pat2 = /^[a-z]+( +[a-z]+)?$/i
var pat3 = /^[0-9]{10}$/;
var pat4 = /^[0-9]{3} [0-9]{2} [0-9]{4}$/
var pat5 = /^\([0-9]{3}\) [0-9]{3} [0-9]{4}$/

Comments

1

This should do it:

var arr =  ["Louis", "Louis Python", "1234567890", "123 45 6848", "(123) 456 1234"];


    var str1 = arr[0];
    var pat1 = /^[a-z]+$/i;
    var first = pat1.test(str1);
    console.log(first);

    var str2 = arr[1];
    var pat2 =/^[a-z]+(?:\s[a-z]+)?$/i;
    var second = pat2.test(str2);
    console.log(second);

    var str3 = arr[2];
    var pat3 = /^[\d]{10}$/;
    var third = pat3.test(str3);
    console.log(third);

    var str4 = arr[3];
    var pat4 = /^[\d]{3}\s[\d]{2}\s[\d]{4}$/;
    var fourth = pat4.test(str4);
    console.log(fourth);

    var str5 = arr[4];
    var pat5 = /^\([\d]{3}\)\s[\d]{3}\s[\d]{4}$/;
    var fifth = pat5.test(str5);
    console.log(fifth);

Output:

true
true
true
true
true

DEMO

http://codepen.io/anon/pen/gpboLr

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.