JavaScript Questions & Answers – Pattern Matching and Regular Expressions

«
»

This set of Javascript Multiple Choice Questions & Answers (MCQs) focuses on “Pattern Matching and Regular Expressions”.

1. The ‘$’ present in the RegExp object is called a ____________
a) character
b) matcher
c) metacharacter
d) metadata
View Answer

Answer: c
Explanation: The ‘S’ is a special metacharacter that matches the end of a string. It is used to define or access elements in jquery.
advertisement

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)
View Answer

Answer: d
Explanation: The given pattern is applied to the text given in the parenthesis. The test() method tests for a match in a string. This method returns true if it finds a match, otherwise it returns false.
advertisement
advertisement

3. The regular expression to match any one character not between the brackets is __________
a) […]
b) [^]
c) [^…]
d) [\D]
View Answer

Answer: c
Explanation: RegExp defines a special set of character that is used to do manipulation on strings and other variables. The [^…] character class is used to match or draw any one character not between the brackets.
advertisement

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
View Answer

Answer: c
Explanation: The [^…] character class is used to match or draw any one character not between the brackets. One should always be careful while using * and ? as repetition characters as they may match zero instances of whatever precedes them, they are allowed to match nothing.

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
View Answer

Answer: a
Explanation: Using non greedy repetition may not always produce the results you expect. /a+?b/ matches the letter b preceded by the fewest number of a’s possible.
advertisement

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”
View Answer

Answer: a
Explanation: paranthesis () check for the optional presence of the argument in the string. subexpression /java(script)?/ 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
View Answer

Answer: b
Explanation: When a regular expression is successfully matched against a target string, it is possible to extract the portions of the target string that matched any particular paranthesized subpattern. The essential purpose of parantheses in regular expressions is to define subpatterns within the complete pattern.
advertisement

8. The method that performs the search-and-replace operation to strings for pattern matching is _______
a) searchandreplace()
b) add()
c) edit()
d) replace()
View Answer

Answer: d
Explanation: The replace() method performs a search-and-replace operation. It takes a regular expression as its first argument and a replacement string as its second argument.

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
View Answer

Answer: b
Explanation: The split() method can take regular expressions as its arguments. The split() method generally breaks the string on which it is called into an array of substrings, using the argument as a separator.

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
View Answer

Answer: a
Explanation: exec() returns the same kind of array whether or not the regular expression has the global g flag. Recall that match() returns an array of matches when passed a global regular expression. exec(), by contrast, always returns a single match and provides complete information about that match. When exec() is called on a regular expression that has the g flag, it sets the lastIndex property of the regular expression object to the character position immediately following the matched substring. When exec() is invoked a second time for the same regular expression, it begins its search at the character position indicated by the lastIndex property. If exec() does not find a match, it resets lastIndex to 0.

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
View Answer

Answer: b
Explanation: The pattern.matches method tests whether the regular expression matches the pattern. The above code will result into false as string “abcd” is not among a, m, or n.

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
View Answer

Answer: a
Explanation: The above code tests for single occurrence of the character in a particular string. The symbol ? is used along with the “[]” for testing single occurrence.

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
View Answer

Answer: a
Explanation: The above code tests for single occurrence of digit. //d returns true when there is any digits occurring one time.

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
View Answer

Answer: a
Explanation: “+” sign in regex checks for more than one time occurrence of a particular character. It returns true if a character from the set is present more than once.

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
View Answer

Answer: b
Explanation: “^” is used as a negation operator. The above code will print false as “a” is present in the string passed in the argument.

Sanfoundry Global Education & Learning Series – Javascript Programming.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter