Online: 13101
By using ?= quantifier we can matches any string that is followed by a particular character in a specific string.
EXAMPLE: "ear" followed by "rings".
<p>Click the below button to match a string .</p> <input type="button" onclick="mySearch()" value="Search"> <p id="myId"></p> <script> function mySearch() { var a = "Last year is all were wear rings to their right ear?" var b = /ear(?= rings)/g; 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 "Last year is all were wear rings to their right ear? " to the variable a, we can find the the word "ear" in 3 places (year, wear, ear) in a string. But we need to match 'ear' followed by a 'rings' in a string, for that we are using ?= quantifier in the variable b. var b = /ear(?= rings)/g in this ?= quantifier searches in a string where 'ear' is followed by 'rings' with the help of a global search g, and variable r returns the global search result. Onclick of the button "Search" in HTML code fires the function mySearch() in the <script> block at the same time ?= quantifier do the global search for the word 'ear' followed by 'rings' in a string and returns 'ear' from the word 'wear' as a output.
OUTPUT