197). The ‘$’ present in the RegExp object is called a ____________
[A]character
[B]matcher
[C]metacharacter
[D]metadata
Show Answer
Correct Answer: metacharacter
Notes:
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.
198). 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)
Show Answer
Correct Answer: pattern.test(text)
Notes:
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.
199). The regular expression to match any one character not between the brackets is __________
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[A][…]
[B][^]
[C][^…]
[D] [\D]
Show Answer
Correct Answer: [^…]
Notes:
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.
200). What does /[^(]* regular expression indicate?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[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
Show Answer
Correct Answer: Match zero or more characters that are not open parenthesis
Notes:
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.
201). What will be the result when non greedy repetition is used on the pattern /a+?b/?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[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
Show Answer
Correct Answer: Matches the letter b preceded by the fewest number of a’s possible
Notes:
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.
202). What does the subexpression /java(script)?/ result in?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[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”
Show Answer
Correct Answer: It matches “java” followed by the optional “script”
Notes:
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”.
203). What is the most essential purpose of parentheses in regular expressions?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[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
Show Answer
Correct Answer: Define subpatterns within the complete pattern
Notes:
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.
204). The method that performs the search-and-replace operation to strings for pattern matching is _______
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[A]searchandreplace()
[B]add()
[C]edit()
[D]replace()
Show Answer
Correct Answer: replace()
Notes:
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.
206). What would be the result of the following statement in JavaScript using regular expression methods?
var text = "testing: 1, 2, 3";
var pattern = /d+/g;
[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
Show Answer
Correct Answer: Returns [“123″,”456″,”789”]
Notes:
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.
207). 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
Show Answer
Correct Answer: Returns the same kind of array whether or not the regular expression has the global g flag
Notes:
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.
208). What will be the output of the following JavaScript code?
console.log(Pattern.matches("[amn]", "abcd"));
[A]true
[B]false
[C]undefined
[D]a
Show Answer
Correct Answer: false
Notes:
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.
209). What will be the output of the following JavaScript code?
console.log(Pattern.matches("[amn]?", "a"));
[A]true
[B]false
[C]undefined
[D]bcd
Show Answer
Correct Answer: true
Notes:
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.
210). What will be the output of the following JavaScript code?
console.log(Pattern.matches("\\d", "1"));
[A]true
[B]false
[C]undefined
[D]1
Show Answer
Correct Answer: true
Notes:
Answer: a
Explanation: The above code tests for single occurrence of digit. //d returns true when there is any digits occurring one time.
211). What will be the output of the following JavaScript code?
Console.log(Pattern.matches("[adf]+", "a"));
[A]true
[B]false
[C]undefined
[D]0
Show Answer
Correct Answer: true
Notes:
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.
212). What will be the output of the following JavaScript code?
console.log(Pattern.matches("[^abc]", "aemngq"));
[A]true
[B]false
[C]undefined
[D]1
Show Answer
Correct Answer: false
Notes:
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.