CodeLab
Show Output
 
xxxxxxxxxx
22
 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
<meta charset="utf-8">
5
<title>JavaScript Match a Pattern in a Multiline String Using Regular Expression</title>
6
</head>
7
<body>
8
    <script>    
9
    var str = "Color red is more visible than \ncolor blue in daylight.";
10
    var regex1 = /^color/gi;
11
    var regex2 = /^color/gim;
12
    
13
    var matches = str.match(regex1); // pattern matching without multiline flag    
14
    document.write("<p>Found " + matches.length + " matches: " + matches + "</p>");
15
    console.log(matches);
16
    
17
    matches = str.match(regex2); // pattern matching with multiline flag
18
    document.write("<p>Found " + matches.length + " matches: " + matches + "</p>");
19
    console.log(matches);
20
    </script>
21
</body>
22
</html>
 

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