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.
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()
});
javascript jquery html
add a comment |
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.
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()
});
javascript jquery html
add a comment |
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.
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()
});
javascript jquery html
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.
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()
});
javascript jquery html
javascript jquery html
edited Nov 21 '18 at 19:58
carreankush
asked Nov 21 '18 at 19:30
carreankushcarreankush
1587
1587
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
function buildMessages(text) {
let messages = text.split(/d.s/);
messages.shift();
messages.forEach((v)=>{
let msg = v.replace(/,/,'').replace(/sors/,'').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 (e.g.1.
), you will get the preface and each of the steps into an array. - Use the
shift
function on the Array removes the unneeded preface. - Use
forEach
to iterate over the values in the array, clean up the text. - Using
replace
to first remove commas, then removeor
with spaces on either side. - Use
trim
to remove leading and training whitespace.
At this point, your array will have sanitized copy for use in your <li>
elements.
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/
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
add a comment |
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.
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419294%2fhow-to-get-all-the-text-separately-from-the-bracket-using-javascript-regular-exp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
function buildMessages(text) {
let messages = text.split(/d.s/);
messages.shift();
messages.forEach((v)=>{
let msg = v.replace(/,/,'').replace(/sors/,'').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 (e.g.1.
), you will get the preface and each of the steps into an array. - Use the
shift
function on the Array removes the unneeded preface. - Use
forEach
to iterate over the values in the array, clean up the text. - Using
replace
to first remove commas, then removeor
with spaces on either side. - Use
trim
to remove leading and training whitespace.
At this point, your array will have sanitized copy for use in your <li>
elements.
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/
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
add a comment |
function buildMessages(text) {
let messages = text.split(/d.s/);
messages.shift();
messages.forEach((v)=>{
let msg = v.replace(/,/,'').replace(/sors/,'').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 (e.g.1.
), you will get the preface and each of the steps into an array. - Use the
shift
function on the Array removes the unneeded preface. - Use
forEach
to iterate over the values in the array, clean up the text. - Using
replace
to first remove commas, then removeor
with spaces on either side. - Use
trim
to remove leading and training whitespace.
At this point, your array will have sanitized copy for use in your <li>
elements.
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/
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
add a comment |
function buildMessages(text) {
let messages = text.split(/d.s/);
messages.shift();
messages.forEach((v)=>{
let msg = v.replace(/,/,'').replace(/sors/,'').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 (e.g.1.
), you will get the preface and each of the steps into an array. - Use the
shift
function on the Array removes the unneeded preface. - Use
forEach
to iterate over the values in the array, clean up the text. - Using
replace
to first remove commas, then removeor
with spaces on either side. - Use
trim
to remove leading and training whitespace.
At this point, your array will have sanitized copy for use in your <li>
elements.
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/
function buildMessages(text) {
let messages = text.split(/d.s/);
messages.shift();
messages.forEach((v)=>{
let msg = v.replace(/,/,'').replace(/sors/,'').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 (e.g.1.
), you will get the preface and each of the steps into an array. - Use the
shift
function on the Array removes the unneeded preface. - Use
forEach
to iterate over the values in the array, clean up the text. - Using
replace
to first remove commas, then removeor
with spaces on either side. - Use
trim
to remove leading and training whitespace.
At this point, your array will have sanitized copy for use in your <li>
elements.
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/
edited Nov 21 '18 at 21:56
answered Nov 21 '18 at 20:02
CodeTheInternetCodeTheInternet
14
14
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
add a comment |
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
1
1
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
If you explain what you changed and why yours works this could be a good answer. As it is, a code-dump with no explanation is not particularly useful to future readers.
– Stephen P
Nov 21 '18 at 20:05
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
can you please use simple way,I think we need to fix only regular expression here regex = /.+(|d. w+/g; to resolve this issue
– carreankush
Nov 21 '18 at 20:23
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:34
add a comment |
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.
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
add a comment |
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.
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
add a comment |
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.
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.
edited Nov 22 '18 at 20:34
answered Nov 21 '18 at 20:10
HexxefirHexxefir
46849
46849
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
add a comment |
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
I replaced my script with your code $(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); matches[0] = matches[0].slice(0, -1); $.each(matches, function(idx, match) { var li = $('<li/>').text(match); $('.messages').append(li); }); }); still I am getting same output,I am getting only first word of text inside brackets Welcome to project, are you a here 1. new 2. test 3. minor 4. Major
– carreankush
Nov 21 '18 at 20:19
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;
– carreankush
Nov 21 '18 at 20:21
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
We need to fix only regular expression here regex = /.+(|d. w+/g;No need to use loop here that will create problem in my application.just I need to get whole value like - Welcome to project, are you a here 1. new user 2. test user 3. minor Accident 4. Major Accident
– carreankush
Nov 21 '18 at 20:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419294%2fhow-to-get-all-the-text-separately-from-the-bracket-using-javascript-regular-exp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown