I am trying to check the following for regex, but cannot make it work. I am using regex101, but can anyone advise how to fix this or format the regular expression?
I have an array - name it arr [] with 5 elements.
- arr [0] must be a variable number of letters, that is, a one-word name.
- arr [1] must be either a one-word or two-word name (that is, a variable number of letters or a variable number of letters followed by a white space followed by a different number of letters)
- arr [2] must be 10 digits in a row (for example, 123432908623, can also start with 0)
- arr [3] must be a social security number with the following format: 3 digits followed by a space followed by 2 digits and then 4 digits (for example, 123 45 6848).
- arr [4] should be a phone number with a city code with the following format: an open bracket, then 3 digits, followed by a closed bracket, followed by a space, followed by an open bracket, and then 3 digits with a closed parenthesis, followed by a space with open brackets, followed by 4 digits, followed by closed brackets (for example, (123) 456 1234).
Here is some code to get you started:
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);
}
source
share