如何使用javascript或jquery将与“逗号”和“或”分开的句子提取到句子的不同部分

时间:2018-11-21 16:32:50

标签: javascript jquery html

我有一个存储在变量中的句子。我需要将该句子解压缩为4个部分,并将其放入代码中的变量中,我可以在此处解压缩并进入控制台,但我不知道如何解压缩' 1。现有用户2.分别测试用户。这是下面的代码。任何人都可以帮助我。

   

$(document).ready(function() {
  regex = /.+\(|\d. \w+ user/g;
  maintext = "Welcome to project, are you a hero(1. Existing user, 2. Test user or 3. new user)";
  matches = maintext.match(regex);
  text_split0 = matches[0].slice(0, -1);
  text_split1 = matches[1];
  text_split2 = matches[2];
  text_split3 = matches[3];
  console.log(text_split0);
  console.log(text_split1);
  console.log(text_split2);
  console.log(text_split3);
  $(".messages").append('<li>'+text_split0+'</li><li>'+text_split1+'</li><li>'+text_split2+'</li><li>'+text_split3+'</li>');
  $("li:contains('undefined')").remove()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   <ul class="messages">

1 个答案:

答案 0 :(得分:0)

$(document).ready(function() {
  regex = /.+\(|\d. \w+ user/g;
  maintext = "Welcome to project, are you (1. Existing user 2. Test user or 3. new user)";
  matches = maintext.match(regex);

  text_split0 = matches[0].slice(0, -2);
  text_split1 = matches[1];
  text_split2 = matches[2];
  text_split3 = matches[3];
  console.log(text_split0);
  console.log(text_split1);
  console.log(text_split2);
  console.log(text_split3);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>