JavaScript | RegExp m Modifier

The RegExp m Modifier in JavaScript is used to perform multiline matching. It takes the beginning and end characters (^ and $) as working when taken over multiple lines. It matches the beginning or end of each line. It is case-sensitive.

Syntax:

/regexp/m 

or

new RegExp("regexp", "m")

Example 1: This example searches the word “geeksforgeeks” at the beginning of each line in a string.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript RegExp m Modifier
    </title>
</head>
  
<body style="text-align:center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
      
    <h2>RegExp m Modifier</h2>
      
    <p>
        geeksforgeeks is the computer science
        portal for geeks.
    </p>
      
    <button onclick="geek()">
        Click it!
    </button>
      
    <p id="app"></p>
      
    <script>
        function geek() {
            var str1 = "geeksforgeeks is the computer "
                       + "science portal for geeks.";
            var regex4 = /^geeksforgeeks/gm;
            var match4 = str1.match(regex4);
  
            document.getElementById("app").innerHTML = 
                    "Found " + match4.length
                    + " matches: " + match4;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Clicking the button:
m
After Clicking the button:
m



Example 2: This example searches the word “geeksforgeeks” at the beginning of each line in a string and replaces it with “GEEKSFORGEEKS”.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<head>
    <title>
        JavaScript RegExp m Modifier
    </title>
</head>
  
<body style="text-align:center">
    <h1 style="color:green">
        GeeksforGeeks
    </h1>
      
    <h2>RegExp m Modifier</h2>
      
    <p>
        geeksforgeeks is the computer science
        portal for geeks.
    </p>
      
    <button onclick="geek()">
        Click it!
    </button>
      
    <p id="app"></p>
      
    <script>
        function geek() {
            var str1 = "geeksforgeeks is the computer "
                       + "science portal for geeks.";
            var regex4 = new RegExp("^geeksforgeeks", "m");         
            var replace = "GEEKSFORGEEKS";
            var match4 = str1.replace(regex4, replace);
            document.getElementById("app").innerHTML = 
            " New string: " + match4;
        }
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Clicking the button:
m
After Clicking the button:
m

Supported Browsers: The browsers supported by RegExp m Modifier are listed below:

  • Google Chrome
  • Apple Safari
  • Mozilla Firefox
  • Opera
  • Internet Explorer

full-stack-img




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.