![]() ![]() The test () method will return true if the string contains any letters and false otherwise. In the example program below, we use the string includes() method to check whether the string “ Tutorialspoint: Simply Easy Learning” contains a substring “ JavaScript” or not. Check if a String contains any Letters in JavaScript Use the RegExp.test () method to check if a string contains any letters, e.g. Syntax string.includes(searchvalue, start) Parameters This method is not supported in Internet Explorer 11 (or earlier). It returns true if a string contains a specified substring otherwise it returns false. The string includes() method is introduced in ES6. This will produce the following result - Contains point 3. ![]() In the example program below, we use string indexOf() method to check whether the string “ Tutorialspoint: Free Video Tutorial” contains a substring “ point” or not. ![]() Start - the index to start from (default is 0). Syntax string.indexOf(searchvalue, start) Parameters The string indexOf() method is an ES1 feature. This method cannot search against a regular expression. The string indexOf() method is case-sensitive. It returns the position of the first occurrence of a value (substring) in the string otherwise it returns -1 if the value is not found. The string indexOf() method can be used to check if a string contains a substring or not. This will produce the following result - Contains Point!" 2. Var str = "Tutorialspoint: Free Video Tutorials" ĭocument.write("Does not contain Point" ) You can try to run the following code to check whether the string “ Tutorialspoint: Free Video Tutorial” contains a substring “point” in or not. ![]() SearchValue is a regular expression or a string (will be converted to a regular expression). The string search() method is an ES1 feature. It matches the string against a regular expression and returns the index (position) of the first match. The string search method can be used to search a text (using regular expression) in a string. In this article, we use the following three methods for this purpose. JavaScript provides us with different methods to check whether a string contains a substring or not. ![]()
0 Comments
Leave a Reply. |