JavaScript > Regular Expressions Modifiers

Starts with search in JavaScript

How to do a multi line search for a specified character at the beginning of each line in a string in JavaScript?


By using JavaScript Regular Expression m Modifier we can do a multi line search for a specified character at the beginning or ending of each line in a string.

<p>Click the below button to do multi line seacrh.</p>
<input type="button" value="Click" onclick="mySearch()" />
<p id="myId"></p>

<script>
    function mySearch() {
        var a = "\nTechFunda is sister a\nsister concern of DotNetFunda";
        var b = /^si/m;
        var r = a.match(b);
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function mySearch() in the<script>block which is connected to the Onclick of the HTML button and there is a string with value "\nTechFunda is sister a \nsister concern of DotNetFunda" to the variable a, from the string we need to do a multi line search for a new line begining character 'si' in a string, for that we are using m modifier in the variable b, variable R returns the multi line search result. Onclick of the button "Click" in the HTML page fires the function mySearch() in the<script> block, at the same time m modifier do the multi line search for a character 'si' at the begining of a new line in a string and returns the output.

OUTPUT

 Views: 3609 | Post Order: 140



Write for us






Hosting Recommendations