JavaScript > Regular Expressions Modifiers

Finding unmatched characters in JavaScript

How to find any character not between the brackets in JavaScript?

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

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

    function mySearch() {
        var a = "TechFunda and 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 DotNetFunda" to the variable a, we need to find the characters not between the brackets, for that we are using [^TF] in varaible b, var b = /[^TF]/g in this code  g do the global search for the characters not between the brackets [^TF]. Variable R returns all the characters except the mentioned characters in brackets [^TF] as output. Onclick of the button "Click" in the HTML code fires the function mySearch() in the <script> block, at the same time [^abc] expression finds characters not between the brackets from a string with the help of global search g  and returns the output.


NOTE: The above output is showing the full length of the string except the mentioned characters [^TF]. Each Character is seperated by comma operator.

 Views: 8209 | Post Order: 142

Write for us

Hosting Recommendations