Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Search a String for a Substring or Pattern in JavaScript</title> </head> <body> <script> // Sample string var str = "Color red looks brighter than color blue." // Search the string for a match var index = str.search(/color/i); if(index !== -1){ document.write("Substring found!"); } else{ document.write("Substring not found!"); } </script> </body> </html>