Regex Matching Variables

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);
}
+4
source share
3 answers

Your code shows a significant number of typos. Also, this seems like the question asked, so I will leave it to you as an additional exercise to understand what the fixes 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]));
}
+2

:

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

:

true
true
true
true
true

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

+1

I believe that using regex in a global format is more interesting, made 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}$/
+1
source

Source: https://habr.com/ru/post/1584948/


All Articles