JavaScript Questions & Answers – Pattern Matching and Regular Expressions | JavaScript MCQs

Sports GK Questions and Answers 2024 (Latest Updated)

Awards & Honours GK Questions 2024 (Latest Updated)

Questions
1 The ‘$’ present in the RegExp object is called a ____________
A character
B matcher
C metacharacter
D metadata

Answer: metacharacter
2 Consider the following JavaScript statement containing regular expressions and check if the pattern matches?
var text = "testing: 1, 2, 3"; 
var pattern = /\d+/g;
A text==pattern
B text.equals(pattern)
C text.test(pattern)
D pattern.test(text)

Answer: pattern.test(text)
Advertisement
Article and Schedule Quiz Start Test!
DOWNLOAD CURRENT AFFAIRS PDF FROM APP
3 The regular expression to match any one character not between the brackets is __________
A […]
B [^]
C [^…]
D [\D]

Answer: [^…]
4 What does /[^(]* regular expression indicate?
A Match one or more characters that are not open parenthesis
B Match zero or more characters that are open parenthesis
C Match zero or more characters that are not open parenthesis
D Match one or more characters that are open parenthesis

Answer: Match zero or more characters that are not open parenthesis
5 What will be the result when non greedy repetition is used on the pattern /a+?b/?
A Matches the letter b preceded by the fewest number of a’s possible
B Matches the letter b preceded by any number of a
C Matches letter a preceded by letter b, in the stack order
D Matches letter a present in the string

Answer: Matches the letter b preceded by the fewest number of a’s possible
6 What does the subexpression /java(script)?/ result in?
A It matches “java” followed by the optional “script”
B It matches “java” followed by any number of “script”
C It matches “java” followed by a minimum of one “script”
D It matches “java” followed by a single “script”

Answer: It matches “java” followed by the optional “script”
7 What is the most essential purpose of parentheses in regular expressions?
A Define pattern matching techniques
B Define subpatterns within the complete pattern
C Define portion of strings in the regular expression
D matching the complete string

Answer: Define subpatterns within the complete pattern
8 The method that performs the search-and-replace operation to strings for pattern matching is _______
A searchandreplace()
B add()
C edit()
D replace()

Answer: replace()
9 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

Answer: Returns [“123″,”456″,”789”]
10 What will be the purpose of exec() in the following JavaScript code?
var pattern = /Java/g;
    var text = "JavaScript is more fun than Java!";
    var result;
    while ((result = pattern.exec(text)) != null) 
    {
        alert("Matched '" + result[0] + "'" +" at position " + result.index +"; 
              next search begins at " + pattern.lastIndex);
    }
A Returns the same kind of array whether or not the regular expression has the global g flag
B Returns different arrays in the different turns of iterations
C Return a sub part of the array
D Returns a null value

Answer: Returns the same kind of array whether or not the regular expression has the global g flag
11 What will be the output of the following JavaScript code?
System.out.println(Pattern.matches("[amn]", "abcd"));
A true
B false
C undefined
D a

Answer: false
12 What will be the output of the following JavaScript code?
System.out.println(Pattern.matches("[amn]?", "a"));
A true
B false
C undefined
D bcd

Answer: true
13 What will be the output of the following JavaScript code?
System.out.println(Pattern.matches("\\d", "1"));
A true
B false
C undefined
D 1

Answer: true
14 What will be the output of the following JavaScript code?
 System.out.println(Pattern.matches("[adf]+", "a"));
A true
B false
C undefined
D 0

Answer: true
15 What will be the output of the following JavaScript code?
 System.out.println(Pattern.matches("[^abc]", "aemngq"));
A true
B false
C undefined
D 1

Answer: false

Random GK Questions

Assam Direct Recruitment Test Series

Teacher Eligibility Test

Assam Direct Recruitment Test Series