将方括号中的所有数字推入数组

时间:2017-12-10 11:42:40

标签: javascript regex

我已经尝试/搜索了很多,但无法解决我的问题。我想这很容易,有些人可以给我一个提示/链接。

我想将网页上方括号[123]中的所有数字转移到如下变量:var all_sources_url =“123,123,123,123,”并将变量附加到链接。

网页示例:

<h1>Text</h1>
<p>For a generative propagation, seeds must be harvested in winter from high quality sites in the nearby area or a site with similar growing conditions [682, 683].</p>
<p>Germination is increased by frost. Germination rates about 80 % can be achieved with good seeds [684].</p>

链接示例

<h1>Link</h1>
<p><a href="" target="_blank" id="all_sources">List of all cited literarure</a></p>

JS试验(如果变量看起来如上所述它会起作用,但构造变量不起作用。)

 <script type="text/javascript">
  $(document).ready(function() {
   var all_sources_url;       
   $.each(($("p").html().match(/\[(.+?)\]/g)), function( index, value){
    all_sources_url += value;
   });
  $("#all_sources").attr("href", "quellen.php?id=" + all_sources_url + "&name=literature");
 });
 </script>

非常感谢你的帮助! 到

1 个答案:

答案 0 :(得分:-1)

 <script type="text/javascript">
  $(document).ready(function() {
   var all_sources_url = [];       
   $.each(($("p").html().match(/\[(.+?)\]/g)), function( index, value){
    all_sources_url.push(value.replace("[", "").replace("]", ""));
   });
  $("#all_sources").attr("href", "quellen.php?id=" + all_sources_url.join(",") + "&name=literature");
 });
 </script>

希望这有帮助!