简体   繁体   English

如何使用javascript正则表达式从括号中分别获取所有文本

[英]How to get all the text separately from the bracket using javascript regular expression

I have a sentence stored in a variable.That sentence I need to extract into 4 parts depends on sentence which I have put into variables in my code,I can able to extract here and get into console but I am not getting the whole text of inside the bracket,only I am getting first words.Here is the code below.Can anyone please help me. 我有一个存储在变量中的句子。我需要将其提取为4个部分,这取决于我在代码中放入变量的句子,我可以在此处提取并进入控制台,但是我没有得到在括号内,只有我要说的是第一句话。这是下面的代码。任何人都可以帮助我。

HTML HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="messages">

SCRIPT 脚本

$(document).ready(function() {
  regex = /.+\(|\d. \w+/g;
  maintext = "Welcome to project, are you a here(1. new user , 2. test user , 3. minor Accident or 4. Major Accident)";
  matches = maintext.match(regex);
  text_split0 = matches[0].slice(0, -1);
  text_split1 = matches[1];
  text_split2 = matches[2];
  text_split3 = matches[3];
  text_split4 = matches[4];
  console.log(text_split0);
  console.log(text_split1);
  console.log(text_split2);
  console.log(text_split3);
  console.log(text_split4);
  $(".messages").append('<li>'+text_split0+'</li><li>'+text_split1+'</li><li>'+text_split2+'</li><li>'+text_split3+'</li><li>'+text_split4+'</li>');
 // $("li:contains('undefined')").remove()
});
function buildMessages(text) {
    let messages = text.split(/\d\.\s/);
    messages.shift();
    messages.forEach((v)=>{
        let msg = v.replace(/\,/,'').replace(/\sor\s/,'').trim();
        $('.messages').append(`<li>${msg}</li>`);
        // console.log(`<li>${msg}</li>`);
    });
}

let sentenceToParse = "Welcome to project, are you a here(1. new user , 2. test user , 3. minor Accident or 4. Major Accident)";

buildMessages(sentenceToParse);
  • Use the split function on the String, keying on the digits (eg 1. ), you will get the preface and each of the steps into an array. 使用String上的split函数,键入数字(例如1. ),您将获得序言和每个步骤进入一个数组。
  • Use the shift function on the Array removes the unneeded preface. 使用Array上的shift功能可删除不需要的前言。
  • Use forEach to iterate over the values in the array, clean up the text. 使用forEach遍历数组中的值,清理文本。
  • Using replace to first remove commas, then remove or with spaces on either side. 使用replace先删除逗号,然后删除or在两侧留空格。
  • Use trim to remove leading and training whitespace. 使用trim删除前导空格和训练空格。

At this point, your array will have sanitized copy for use in your <li> elements. 此时,您的数组将具有已清理的副本,可在您的<li>元素中使用。

If you're only concerned with working through a regex and not re-factoring, the easiest way may be to use an online regex tool where you provide a few different string samples. 如果您只关心通过正则表达式而不是进行重构,则最简单的方法可能是使用在线正则表达式工具,在其中提供一些不同的字符串样本。 Look at https://www.regextester.com/ 看看https://www.regextester.com/

Ok, Try another approach, cause regex for this isn't the best way. 好吧,尝试另一种方法,因为正则表达式不是最好的方法。 Try this: 尝试这个:

$(document).ready(function() {
  // First part of sentence.
  var mainText = "Welcome to project, are you a here(";
  // Users array.
  var USERS = ['new user', 'test user', 'minor Accident', 'Major Accident'];
  var uSize = USERS.length;

  // Construct string & user list dynamically.
  for(var i = 0; i < uSize; i++) {
    var li = $('<li/>').text(USERS[i]);

    if(i === uSize - 1)
      mainText += (i+1) + ". " + USERS[i] + ")";
    else if(i === uSize - 2)
      mainText += (i+1) + ". " + USERS[i] + " or ";
    else
      mainText += (i+1) + ". " + USERS[i] + " , ";

    $(".messages").append(li);
  }
  console.log(mainText); // You will have you complete sentence.
}

Why that way is better? 为什么这样更好? Simple, you can add or remove users inside the user array. 很简单,您可以在用户数组中添加或删除用户。 String together with your user list will be updated automatically. 字符串以及您的用户列表将自动更新。 I hope that help you. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript括号中的正则表达式 - Regular expression in Javascript bracket 使用正则表达式获取第二个括号内容 - Get the second bracket content using regular expression 如何使用正则表达式或纯 JavaScript 从字符串中删除所有括号及其内容并保留其他文本格式 - How to remove all parenthesis and their contents from a string using a regular expression or pure JavaScript and retain other text format 如何使用 JavaScript 正则表达式拆分此文本? - How to split this text using JavaScript regular expression? 如何使用正则表达式从字符串地址获取特定文本? - How to Get Specific text from string address using regular expression? 如何使用javascript正则表达式从字符串中获取域 - How to get domain from a string using javascript regular expression Javascript正则表达式 - 在[这里的东西]之后获取所有文本 - Javascript regular expression - get all text after [stuff in here] Javascript正则表达式可从CSS获取所有媒体查询 - Javascript Regular expression to get all media queries from Css Javascript正则表达式从字符串文本中获取时间 - Javascript Regular expression to get the Time from the string text 如何使用正则表达式获取所有 URL src<Img> JavaScript 中的标签? - How to Regular Expression to get all URL src in <Img> tag in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM