

Finally, call this method with two different words as sub-string to the above main string.The forward slashes / / mark the beginning and end of the regular expression. We called the RegExp.test method to check if the string contains only letters and numbers. In this example, our main string is “Hello World”. Check if a String contains any Letters in JavaScript If you need to also allow spaces, hyphens, underscores, or other characters, scroll down to the next code snippet.Print out the message to the user accordingly. If it is greater than or equal to 0, means that the substring exists inside the given string.


It prints out the result based on subString exists in mainString or not. The includes () method checks whether a JavaScript string contains a substring and returns a boolean. subString is the sub-string and mainString is the main string. doesExist method takes two parameters.The commented numbers in the above program denote the step number below : 1 function doesExist ( subString, mainString ) //3 var mainString = "Hello World" //4 doesExist ( "Hello", mainString ) doesExist ( "Universe", mainString ) Explanation :
