How to pass a variable in match function in javascript?

html, javascript, jquery, regex

Solution

Here is how:

var re = new RegExp(str1, "gi");

Problem

Firstly this may sound like a duplicate question but I was unable to resolve my problem using the previous asked questions: This is my HTML page: ``` <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <script> function test() { var totalids='XYZ@@'; var variable_name=document.getElementById('p1').value.replace(/^\s+|\s+$/g,''); var result = totalids.match(/variable_name/i); if (result){ alert('Matched'); } else { alert("Not Matched"); } } </script> </head> <body> <input type="button" value="click on me" onclick="test()"> <input type="text" value="xyz" id="p1"> </body> </html> ``` This program is not matching my String.I want to match my string using case-insensitive match.I know the problem lies in this line: ``` var result = totalids.match(/variable_name/i); ``` I am unable to pass variable in match function.I can do this; ``` var re = new RegExp(str1, "g"); ``` But I don't know how to do cases-insensitive search using above line.

Original source