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);
}