Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript的match函数中传递变量?_Javascript_Jquery_Html_Regex - Fatal编程技术网

如何在javascript的match函数中传递变量?

如何在javascript的match函数中传递变量?,javascript,jquery,html,regex,Javascript,Jquery,Html,Regex,首先,这听起来像是一个重复的问题,但我无法使用之前提出的问题解决我的问题: 这是我的HTML页面: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> <script> function t

首先,这听起来像是一个重复的问题,但我无法使用之前提出的问题解决我的问题: 这是我的HTML页面:

<!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>
我无法在匹配函数中传递变量。我可以这样做

 var re = new RegExp(str1, "g");
但我不知道如何使用上述行进行不区分大小写的搜索。

以下是如何:

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

您可以使用:
var re=newregexp(变量名称,“gi”)使其成为现实insensitive@anubhava谢谢你,伙计。我欠你这个人情。我应该删除我的问题吗?现在我觉得这是一个非常愚蠢的问题。我不这么认为!但这是你的决定。在学习的过程中,我们都会犯这样的错误,这没关系。这是学习的一部分。。。现在你可能永远不会忘记。好吧,我不会,也许它会对其他人有帮助。如果它不是复制品,它肯定会留下来。这是一个常见的问题,答案将帮助很多人。
var re = new RegExp(str1, "gi");