CodeLab
Show Output
 
xxxxxxxxxx
21
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>Check If a String Contains a Substring in JavaScript</title>
6
</head>
7
<body>
8
    <script>
9
    // Sample string
10
    var str = "The quick brown fox jumps over the lazy dog."
11
    
12
    // Check if the substring exists inside the string
13
    var index = str.indexOf("fox");    
14
    if(index !== -1){
15
        document.write("Substring found!");
16
    } else{
17
        document.write("Substring not found!");
18
    }
19
    </script>
20
</body>
21
</html>
 

在微博、微信、公众号、小程序分享此示例。 如果您觉得有帮助,请给我们一个赞。