Updated February 18, 2023
- It can perform a search operation to find an equivalent between the regular expression and the given string which can give output in true or false.
- The test() method has been used whenever we desire to know whether a pattern is established in the string.
- It gives output in Boolean.
Let us see the example to understand how to use the RegEx test method:
console.log ("Equal String: ",/map/i.test('Informative')):
console.log ("Case Sensitive: ",/MAP/.test(' Informative')):
console.log ("Non-equal String: ",/ive2/.test(' Informative'));
In the above three examples, we have used the test method to find out if there are equal. the test then it will give back the Boolean value in the output.
We conclude that the ‘/map/i’ is a regular expression, ‘map’ is a pattern that can be utilized in the search, and ‘i’ is the modifier that authorizes the search to be case sensitive.
- In Ex.1, the ‘map’ is existing in the string of ‘Informative’, hence it will give back a ‘true’.
- In Ex.2, MAP is existing but we have not appended the ‘i’ modifier for case sensitiveness hence it will give back a ‘false’.
- In Ex.3, ‘ive2’ is not existing in the given string of ‘Informative’ so the test will give back the ‘false’.
- text.match(pattern) – Will be the string method match()
- text.search(pattern) – Will be the string method search()
- pattern.exec(text) – Will be the RegExp method exec()
- Pattern.test(text) – Will be the RegEx method test ()
Given below is the syntax mentioned:
- RegExObject: Is the class which can act with the regular expression.
- String and RegEx: Both can describe the method which can utilize the regular expression to carry out significant searching of a string but the string is a compulsory field.
RegEx is the regular expression in which we can define a pattern in a string data. It authorizes us to examine a string such as an address or password for patterns in which we can able to understand whether the pattern is described in it that able to generate actionable information or not.
The test() method for the RegEx can carry out selecting an equal between a regular expression and the given string so if a given equal string is found then it will give back the true result and if not found then it will give out a false result, it has mainly two parameters such as string and RegEx.
- RegEx: It is the name of the regular expression.
- String: It is a string that can be utilized as opposed to a regular expression which is the required field.
Given below is the example mentioned:
This example looks for the string “the” in the actual string.
Let us see an example of looking at the string ‘Method’ in the actual string.