[英]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个部分,这取决于我在代码中放入变量的句子,我可以在此处提取并进入控制台,但是我没有得到在括号内,只有我要说的是第一句话。这是下面的代码。任何人都可以帮助我。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="messages">
$(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);
split
function on the String, keying on the digits (eg 1.
), you will get the preface and each of the steps into an array. split
函数,键入数字(例如1.
),您将获得序言和每个步骤进入一个数组。 shift
function on the Array removes the unneeded preface. shift
功能可删除不需要的前言。 forEach
to iterate over the values in the array, clean up the text. forEach
遍历数组中的值,清理文本。 replace
to first remove commas, then remove or
with spaces on either side. replace
先删除逗号,然后删除or
在两侧留空格。 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.