正则表达式匹配模式为字符串或其他模式在相同的字符串中

时间:2017-09-26 07:55:09

标签: javascript regex google-tag-manager

我的Google跟踪代码管理器设置需要您的帮助。

如果列表中的项目编号在字符串中被识别,我想触发一个触发器。

让我们举个例子。我的项目清单:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://use.fontawesome.com/c6435311fd.js"></script>

<div class="container">
<h2>Collapsible Panel</h2>
<p>Click on the collapsible panel to open and close it.</p>
<div class="panel-group">
<div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">
     <a href="audits-inspections-surveys.php"><i class="fa fa-angle-down" data-toggle="collapse" href="#collapse1" class="collapsed" aria-expanded="false"></i>Collapsible panel</a>
    </h4>
  </div>
  <div id="collapse1" class="panel-collapse collapse">
    <div class="panel-body">Panel Body</div>
    <div class="panel-footer">Panel Footer</div>
  </div>
  </div>
  </div>
  <div class="panel-group">
 <div class="panel panel-default">
  <div class="panel-heading">
    <h4 class="panel-title">
      <span class="coll" data-toggle="collapse" href="#collapse2"></span><a href="audits-inspections-surveys.php">Collapsible panel</a>
    </h4>
  </div>
  <div id="collapse2" class="panel-collapse collapse">
    <div class="panel-body">Panel Body</div>
    <div class="panel-footer">Panel Footer</div>
    </div>
   </div>
   </div>
    </div>

如果DOM元素在字符串中包含11310 11311 11312 11313 OR 11310 0R 1131111312,则触发器应该触发。

您能否帮助创建相关的RegEx?

由于

1 个答案:

答案 0 :(得分:0)

你可以做到

&#13;
&#13;
function match(str){
    return str.match(/^1131[0-3]$/) != null;
}

console.log(match('11310'));
console.log(match('11311'));
console.log(match('11312'));
console.log(match('11313'));
console.log(match('11314'));
&#13;
&#13;
&#13;