

RegExp.prototype has a test method which returns a boolean. However, that does point us toward something else useful. Unfortunately, with the exception of matching on a regular expression rather than a string, the behavior is identical to indexOf. Check if a string contains another string as a substring in JavaScript 1. Looking through the documentation for String.prototype, the search method looks promising due to its name. Syntax :-string.includes (searchString, position) searchString:-A string to be searched for within this string. Ideally, what we're looking for is a method with a name that matches our intention (determining if x contains y), and returns a simple true or false. The includes () method determines whether one string may be found within another string, returning true or false as appropriate. That means that we can use it, but the clarity of the code suffers. In the event that no match is found, it will return -1. Its job is to return the index at which a given substring is found. While indexOf is often recommended as a simple way to test for the presence of a substring, that's not really its purpose. The test () method will return true if the string contains only. Var philosophers = "Aquinas, Maimonedes, and Avicenna" var me = "Joshua" function printPhilosopherStatus ( person ) // Outputs: "Joshua is NOT a philosopher." printPhilosopherStatus ( me ) To check if a string contains only numbers in JavaScript, call the test () method on this regular expression: \d+.
