Javascript (JS) Regular Expression MCQ
This section focuses on the "Javascript Regular Expression" of the Javascript. These Multiple Choice Questions (mcq) should be practiced to improve the Javascript skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. Which among the following regular expression specifies that password should contain at least one capital letter or one digit.
A. /.*[A-Z].*/.test(password) && /.*[0-9].*/.test(password)
B. /.*[A-Z].*/.test(password) || /.*[0-9].*/.test(password)
C. /.*[A-Z][0-9].*/.test(password)
D. None of the above
View Answer
Ans : B
Explanation: This option is correct because, the regular expression will match if any characters from [A-Z] are there or any numbers from [0-9] are present in the password.
2. Which of the following string will match the RegEx "((d).(dd))$"?
A. 223.123
B. 22.123
C. 223.12
D. 2.2
View Answer
Ans : C
Explanation: This option is correct because, as per the given RegEx it can have any number of digits before the decimal point. But it should have only 2 digits after the decimal point.
3. Consider the following JavaScript statement containing regular expressions and check if the pattern matches?
var text = "lestfindcourse: 1, 2, 3";
var pattern = /d+/g;
A. text==pattern
B. text.equals(pattern)
C. text.test(pattern)
D. pattern.test(text)
View Answer
Ans : D
Explanation: The given patterns are applied to the text given in parentheses. Test () method test for a match in a string. This method is true if it finds a match, otherwise it is false.
4. The regular expression to match any one character not between the brackets is __________
A. [....]
B. [^]
C. [^...]
D. [\D]
View Answer
Ans : C
Explanation: RegExp defines a special set of characters that are used to manipulate strings and other variables. Character class is used to match or draw a single character between brackets.
5. What would be the result of the following statement in JavaScript using regular expression methods?
A. Returns ["123""456""789"]
B. Returns ["123","456","789"]
C. Returns [1,2,3,4,5,6,7,8,9]
D. Throws an exception
View Answer
Ans : B
Explanation: The split () method can take its arguments as regulars. The split () method typically breaks the string, on which it is called as a separator, the argument is used as a separator.
6. What will be the output of the following JavaScript code?
System.out.println( Pattern.matches("[lfc]", "lets") );
A. true
B. false
C. undefined
D. l
View Answer
Ans : B
Explanation: The pattern.matches method tests whether the regular expression matches the pattern. The above code will not result in a false as string "lets" is not among l , f, or c.
7. What will be the output of the following JavaScript code?
System.out.println( Pattern.matches("[lfc]?", "l") );
A. true
B. false
C. undefined
D. l
View Answer
Ans : A
Explanation: The pattern.matches method tests whether a regular expression matches a pattern. The method checks for single occurrence of any letter, and symbol ? Used with "[]" for single event testing.
8. What will be the output of the following JavaScript code?
System.out.println( Pattern.matches("\d", "9") );
A. true
B. false
C. undefined
D. 9
View Answer
Ans : A
Explanation: The above code test for single occurrence of digits. // d is returned once any digit is true.
9. What will be the output of the following JavaScript code?
System.out.println( Pattern.matches("[^lfc]", "letsfind") );
A. true
B. false
C. undefined
D. l
View Answer
Ans : B
Explanation: "^" Is used as a negation operator. The above code will be incorrect as "A" which is present in the string passed in the argument.
10. What will be the output of the following JavaScript code?
System.out.println( Pattern.matches("[lfc]+", "f") );
A. true
B. false
C. undefined
D. 1
View Answer
Ans : A
Explanation: "+" Sign in regex check if a particular character occurs more than once. This is true if a character from the set is present more than once.
Also check :
Discussion