JavaScript > Regular Expressions Modifiers

Finding each characters in JavaScript

How to find any character between the brackets in JavaScript?

By using [abc] expression we can find any character between the brackets.

<p>Click the below button to find the characters between the brackets.</p>
<input type="button" value="Click" onclick="mySearch()" />
<p id="myId"></p>

    function mySearch() {
        var a = "TechFunda and ITFunda is a sister concern of DotNetFunda";
        var b = /[TF]/g;
        var r = a.match(b);
        document.getElementById("myId").innerHTML = r;

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 "TechFunda and ITFunda is a sister concern of DotNetFunda" to the variable a, from the  string we need to find the characters 'T&F' for that we are using [abc] expression in the variable B var B = /[TF]/g, in this code g do the global search for the characters in the brackets [TF]. Variable r returns the mentioned characters in brackets from a string. Onclick of the button "Click" in the HTML code fires the function mySearch() in the <script> block, at the same time [abc] expression finds the characters between the brackets from a string with the help of global search and returns the output.


NOTE: The order of output is T,F,T,F,F. This is due to "TechFunda and ITFunda is a sister concern of DotNetFunda". 

 Views: 3514 | Post Order: 141

Write for us

Hosting Recommendations