Web Development Course:
- This method is used to test for a match inside a string variable.
test()method will return true if a match is found, just like a regular conditional statement. In other case, it will return false.
test() Function Explained
test() function will return a boolean value. If the string contains the characters you typed in, the method will return true. When the string does not contain any matches, false is
The following code snippet illustrates the way the
var sampleString = "Testing testing"; var samplePattern = new RegExp("t"); var sampleResult = samplePattern.test(sampleString);
Write The Method Correctly
.test function will not give you a headache. If you have at least a tiny bit of experience in programming, you will quickly grasp the way the function needs to be included in your code:
regExpObject defines the pattern you will be looking for. Then, there is a required parameter called string which indicates the string to be searched for specified characters or words.
What Method Returns
In our case, true indicates that matches of the pattern you specified were successfully found. However, if the outcome is false, that means no matches were detected in a string.