1. The ‘$’ present in the RegExp object is called a ____________
Explanation: The ‘S’ is a special metacharacter that matches the end of a string. It is used to define or access elements in jquery.
2. Consider the following statement containing regular expressions
var text = "testing: 1, 2, 3"; var pattern = /\d+/g;
In order to check if the pattern matches, the statement is
Explanation: The given pattern is applied on 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.
3. The regular expression to match any one character not between the brackets is __________
Explanation: RegExp define special set of character that are 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.
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
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
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.
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”
Explanation: paranthesis () check for 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 parantheses 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
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.
8. The method that performs the search-and-replace operation to strings for pattern matching is _______
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.
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
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. Consider the following code snippet
What purpose does exec() solve in the above code?
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
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.