I have a below sentence, I need to extract it and get the proper output.
Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)
I am getting the output now using regular expression:-
Welcome to project, are you a here
1. new user
2. test user
3. minor Accident or
4. Major
I should get the output as per sentence using regular expression:-
Welcome to project, are you a here
1. new user one
2. test user two
3. minor Accident one
4. Major Accident
Here is the code below,updated in plnker which I have used,I am not getting solution since I don't have that much idea in redular expression.
$(document).ready(function() { regex = /.+\\(|\\d. [\\w\\s]+ /g; maintext = "Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one 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() });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <ul class="messages"> </ul>
Instead of creating one unicorn regex; do it in two steps. Simplify the regex to just finding the numbers. So you are matching just this:
https://regex101.com/r/BXf1IM/1
"1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident"
Then it will be easier to split that string by the numbers.
list.split(/([\d]. )/g)
Then you can loop through those to append it to an order list.
<ol class="messages">
<li>new user one</li>
<li>test user two</li>
<li>minor Accident one or </li>
<li>Major Accident</li>
</ol>
You'll need to clean up the " or " on the third item.
Full script:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="messages"></ul>
<script>
var text = 'Welcome to project, are you a here(1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident)';
var regex = /\((.*)\)/;
var m;
if ((m = regex.exec(text)) !== null) {
console.log(m[1]);
// 1. new user one, 2. test user two, 3. minor Accident one or 4. Major Accident
var list = m[1];
var items = list.split(/([\d]. )/g);
var message = $('.messages');
// Remove ' or ' from secondLastItem by removing the last 4 characters
var secondLastItem = items[items.length-3];
items[items.length-3] = secondLastItem.substring(0, secondLastItem.length - 4);
for(var i=1;i<items.length;i++) {
// Ordered list
// message.append('<li>' + items[++i].replace(', ', '') + '</li>');
// Unordered list
message.append('<li>' + items[i] + items[++i].replace(', ', '') + '</li>');
}
}
// The intro text
var introText = text.split('(');
console.log(introText[0]);
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.