JOIN ADRE 2.0 Telegram Group

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

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

ADRE 2.0 FULL LENGTH MOCK TEST

Take Mock Tests

Missiles Mock Test Start Test!
SSC MTS Mock Test Start Test
IBPS CLERK MOCK TEST Start Test
SSC MTS 2022 JULY 26 Shift 1 (ENGLISH) Start Test!
SSC GD Previous Year Paper 2021 Nov 17 Shift - I (Hindi) Start Test!
SSC CGL Tier - 1 PYP 2022 April 21 Shift- 1 (ENGLISH) Start Test!
MPSC PAPER I MOCK TEST 1 (ENGLISH) Start Test!
IB Security Assistant Mock test 1 (english) Start Test!
UP POLICE CONSTABLE MOCK TEST 1 Start Test!
DELHI POLICE CONSTABLE MOCK TEST 1 (HINDI) Start Test!
Advertisement
Assam Direct Recruitment Test Series

Latest Test Series

ssc cgl test series

SSC CGL

Start Test 1000+ Tests Latest patternView Course
delhi policesi test series

DELHI POLICE SI

Start Test 1000+ Tests Latest patternView Course
west bengal psc test series

WEST BENGAL PSC

Start Test 1000+ Tests Latest patternView Course
cisf head constable test series

CISF CONSTABLE

Start Test 1000+ Tests Latest patternView Course
Junior Associate of the Indian Institute of Bankers(JAIIB) test series

JAIIB

Start Test 30 Full Length Tests View Course
adre 2024 test series

ADRE 2024

Start Test 25000+ QUESTIONS SOLUTIONSView Course
RRB RRC GROUP D test series

RRB RRC GROUP D

Start Test 15000+ QUESTIONS Latest patternView Course
UP POLICE SI test series

UP POLICE SI

Start Test 16000+ QUESTIONS Latest patternView Course
CLICK HERE TO VIEW ALL COURSES