How to extract text which are outside and inside a bracket from a sentence through javascript or jquery












0















I have a sentence stored in a variable.That sentence I need to extract into 3 parts which I have mentioned in other 3 variable in my code,for now I have hard coded in the variable. Then I need to console those variable separately.I have already tried with slice for first one,is there any better way to do it.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>


SCRIPT



$(document).ready(function(){
var maintext = "Welcome to project,are you (1. Existing user or 2. new user)" ;
var text_split1 = maintext.split(' ').slice(0,4).join(' ');
//var text_split1 = "Welcome to project,are you" ; /*this text should come from maintext after extraction*/
var text_split2 = "1. Existing user" ; /*this text should come from maintext after extraction*/
var text_split3 = "2. new user" ; /*this text should come from maintext after extraction*/
console.log(text_split1);
console.log(text_split2);
console.log(text_split3);
});









share|improve this question




















  • 1





    What have you tried so far? What specifically are you having difficulty with?

    – freedomn-m
    Nov 20 '18 at 16:20








  • 2





    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – freedomn-m
    Nov 20 '18 at 16:21











  • I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

    – carreankush
    Nov 20 '18 at 16:25













  • You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

    – freedomn-m
    Nov 20 '18 at 16:32
















0















I have a sentence stored in a variable.That sentence I need to extract into 3 parts which I have mentioned in other 3 variable in my code,for now I have hard coded in the variable. Then I need to console those variable separately.I have already tried with slice for first one,is there any better way to do it.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>


SCRIPT



$(document).ready(function(){
var maintext = "Welcome to project,are you (1. Existing user or 2. new user)" ;
var text_split1 = maintext.split(' ').slice(0,4).join(' ');
//var text_split1 = "Welcome to project,are you" ; /*this text should come from maintext after extraction*/
var text_split2 = "1. Existing user" ; /*this text should come from maintext after extraction*/
var text_split3 = "2. new user" ; /*this text should come from maintext after extraction*/
console.log(text_split1);
console.log(text_split2);
console.log(text_split3);
});









share|improve this question




















  • 1





    What have you tried so far? What specifically are you having difficulty with?

    – freedomn-m
    Nov 20 '18 at 16:20








  • 2





    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – freedomn-m
    Nov 20 '18 at 16:21











  • I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

    – carreankush
    Nov 20 '18 at 16:25













  • You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

    – freedomn-m
    Nov 20 '18 at 16:32














0












0








0








I have a sentence stored in a variable.That sentence I need to extract into 3 parts which I have mentioned in other 3 variable in my code,for now I have hard coded in the variable. Then I need to console those variable separately.I have already tried with slice for first one,is there any better way to do it.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>


SCRIPT



$(document).ready(function(){
var maintext = "Welcome to project,are you (1. Existing user or 2. new user)" ;
var text_split1 = maintext.split(' ').slice(0,4).join(' ');
//var text_split1 = "Welcome to project,are you" ; /*this text should come from maintext after extraction*/
var text_split2 = "1. Existing user" ; /*this text should come from maintext after extraction*/
var text_split3 = "2. new user" ; /*this text should come from maintext after extraction*/
console.log(text_split1);
console.log(text_split2);
console.log(text_split3);
});









share|improve this question
















I have a sentence stored in a variable.That sentence I need to extract into 3 parts which I have mentioned in other 3 variable in my code,for now I have hard coded in the variable. Then I need to console those variable separately.I have already tried with slice for first one,is there any better way to do it.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>


SCRIPT



$(document).ready(function(){
var maintext = "Welcome to project,are you (1. Existing user or 2. new user)" ;
var text_split1 = maintext.split(' ').slice(0,4).join(' ');
//var text_split1 = "Welcome to project,are you" ; /*this text should come from maintext after extraction*/
var text_split2 = "1. Existing user" ; /*this text should come from maintext after extraction*/
var text_split3 = "2. new user" ; /*this text should come from maintext after extraction*/
console.log(text_split1);
console.log(text_split2);
console.log(text_split3);
});






javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 16:29







carreankush

















asked Nov 20 '18 at 16:18









carreankushcarreankush

1607




1607








  • 1





    What have you tried so far? What specifically are you having difficulty with?

    – freedomn-m
    Nov 20 '18 at 16:20








  • 2





    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – freedomn-m
    Nov 20 '18 at 16:21











  • I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

    – carreankush
    Nov 20 '18 at 16:25













  • You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

    – freedomn-m
    Nov 20 '18 at 16:32














  • 1





    What have you tried so far? What specifically are you having difficulty with?

    – freedomn-m
    Nov 20 '18 at 16:20








  • 2





    developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – freedomn-m
    Nov 20 '18 at 16:21











  • I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

    – carreankush
    Nov 20 '18 at 16:25













  • You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

    – freedomn-m
    Nov 20 '18 at 16:32








1




1





What have you tried so far? What specifically are you having difficulty with?

– freedomn-m
Nov 20 '18 at 16:20







What have you tried so far? What specifically are you having difficulty with?

– freedomn-m
Nov 20 '18 at 16:20






2




2





developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– freedomn-m
Nov 20 '18 at 16:21





developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

– freedomn-m
Nov 20 '18 at 16:21













I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

– carreankush
Nov 20 '18 at 16:25







I have tried slice,but if there any other better way to fix this, var text_split1 = maintext.split(' ').slice(0,4).join(' ');

– carreankush
Nov 20 '18 at 16:25















You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

– freedomn-m
Nov 20 '18 at 16:32





You need to explicitly state your rules. eg text1 is text before first (, text2 is inside the bracket, before and no including "or", etc. Otherwise it's just guesswork and only fit for this exact scenario - which your existing code is also; only fit for this exact scenario.

– freedomn-m
Nov 20 '18 at 16:32












2 Answers
2






active

oldest

votes


















1














The best option in this case is to create a regular expression.



In your case (if I got it right) you have a sentence consisting of:




  1. Some text

  2. Open parenthesis '('

  3. More text

  4. The word 'or'

  5. More text

  6. Close parenthesis ')'


So you want a regex to match the format above and you want to capture the items 1, 3 and 5.



Here, I made the regex for you: /^(.*) ?((.*) or (.*))$/



Now, using the function exec(string) you can match the desired string and capture the desired sections from it.



regex = /^(.*) ?((.*) or (.*))$/;
maintext = "Welcome to project,are you (1. Existing user or 2. new user)";
matches = regex.exec(maintext);
text_split1 = matches[1];
text_split2 = matches[2];
text_split3 = matches[3];


Notice that I didn't use matches[0]. That's because this is the full match from the regex, which, in this case, would be the full string you had at first.



Also, notice that the regex may fail with any of the texts inside the parenthesis contain the full word 'or', which is being used to separate the two portions of the string. You must secure this won't happen, or maybe use some special character to separate the two texts instead of the word.






share|improve this answer



















  • 1





    Thanks for you help

    – carreankush
    Nov 20 '18 at 16:59











  • You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

    – freedomn-m
    Nov 20 '18 at 19:34











  • @freedomn-m, I used whitespaces.

    – Pedro Lima
    Nov 21 '18 at 1:52











  • s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

    – freedomn-m
    Nov 21 '18 at 8:43











  • I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

    – carreankush
    Nov 21 '18 at 16:12



















0














I suggest you take a look at the Split, Substring and IndexOf methods.



For example if you wanted to grab what's inside the parenthesis you could do



var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) )


and then whats outside of it



var outside_split = maintext.substring(0, maintext.indexOf("(") - 1)


or with Split



var split_Text = maintext.split("(") 


Careful with Split here because it'll result in the last parenthesis being included in.



Array [ "Welcome to project,are you ", "1. Existing user or 2. new user)" ]


and from that you can Split the two options with



split_Text[1].split("or")


which results in



Array [ "1. Existing user ", " 2. new user)" ]


and then manipulate the remaining closing parenthesis






share|improve this answer


























  • var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

    – carreankush
    Nov 20 '18 at 16:41











  • Did you read the rest of the post? You can pass "or" to Split

    – Matheus Souza
    Nov 20 '18 at 16:44











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397205%2fhow-to-extract-text-which-are-outside-and-inside-a-bracket-from-a-sentence-throu%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









1














The best option in this case is to create a regular expression.



In your case (if I got it right) you have a sentence consisting of:




  1. Some text

  2. Open parenthesis '('

  3. More text

  4. The word 'or'

  5. More text

  6. Close parenthesis ')'


So you want a regex to match the format above and you want to capture the items 1, 3 and 5.



Here, I made the regex for you: /^(.*) ?((.*) or (.*))$/



Now, using the function exec(string) you can match the desired string and capture the desired sections from it.



regex = /^(.*) ?((.*) or (.*))$/;
maintext = "Welcome to project,are you (1. Existing user or 2. new user)";
matches = regex.exec(maintext);
text_split1 = matches[1];
text_split2 = matches[2];
text_split3 = matches[3];


Notice that I didn't use matches[0]. That's because this is the full match from the regex, which, in this case, would be the full string you had at first.



Also, notice that the regex may fail with any of the texts inside the parenthesis contain the full word 'or', which is being used to separate the two portions of the string. You must secure this won't happen, or maybe use some special character to separate the two texts instead of the word.






share|improve this answer



















  • 1





    Thanks for you help

    – carreankush
    Nov 20 '18 at 16:59











  • You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

    – freedomn-m
    Nov 20 '18 at 19:34











  • @freedomn-m, I used whitespaces.

    – Pedro Lima
    Nov 21 '18 at 1:52











  • s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

    – freedomn-m
    Nov 21 '18 at 8:43











  • I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

    – carreankush
    Nov 21 '18 at 16:12
















1














The best option in this case is to create a regular expression.



In your case (if I got it right) you have a sentence consisting of:




  1. Some text

  2. Open parenthesis '('

  3. More text

  4. The word 'or'

  5. More text

  6. Close parenthesis ')'


So you want a regex to match the format above and you want to capture the items 1, 3 and 5.



Here, I made the regex for you: /^(.*) ?((.*) or (.*))$/



Now, using the function exec(string) you can match the desired string and capture the desired sections from it.



regex = /^(.*) ?((.*) or (.*))$/;
maintext = "Welcome to project,are you (1. Existing user or 2. new user)";
matches = regex.exec(maintext);
text_split1 = matches[1];
text_split2 = matches[2];
text_split3 = matches[3];


Notice that I didn't use matches[0]. That's because this is the full match from the regex, which, in this case, would be the full string you had at first.



Also, notice that the regex may fail with any of the texts inside the parenthesis contain the full word 'or', which is being used to separate the two portions of the string. You must secure this won't happen, or maybe use some special character to separate the two texts instead of the word.






share|improve this answer



















  • 1





    Thanks for you help

    – carreankush
    Nov 20 '18 at 16:59











  • You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

    – freedomn-m
    Nov 20 '18 at 19:34











  • @freedomn-m, I used whitespaces.

    – Pedro Lima
    Nov 21 '18 at 1:52











  • s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

    – freedomn-m
    Nov 21 '18 at 8:43











  • I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

    – carreankush
    Nov 21 '18 at 16:12














1












1








1







The best option in this case is to create a regular expression.



In your case (if I got it right) you have a sentence consisting of:




  1. Some text

  2. Open parenthesis '('

  3. More text

  4. The word 'or'

  5. More text

  6. Close parenthesis ')'


So you want a regex to match the format above and you want to capture the items 1, 3 and 5.



Here, I made the regex for you: /^(.*) ?((.*) or (.*))$/



Now, using the function exec(string) you can match the desired string and capture the desired sections from it.



regex = /^(.*) ?((.*) or (.*))$/;
maintext = "Welcome to project,are you (1. Existing user or 2. new user)";
matches = regex.exec(maintext);
text_split1 = matches[1];
text_split2 = matches[2];
text_split3 = matches[3];


Notice that I didn't use matches[0]. That's because this is the full match from the regex, which, in this case, would be the full string you had at first.



Also, notice that the regex may fail with any of the texts inside the parenthesis contain the full word 'or', which is being used to separate the two portions of the string. You must secure this won't happen, or maybe use some special character to separate the two texts instead of the word.






share|improve this answer













The best option in this case is to create a regular expression.



In your case (if I got it right) you have a sentence consisting of:




  1. Some text

  2. Open parenthesis '('

  3. More text

  4. The word 'or'

  5. More text

  6. Close parenthesis ')'


So you want a regex to match the format above and you want to capture the items 1, 3 and 5.



Here, I made the regex for you: /^(.*) ?((.*) or (.*))$/



Now, using the function exec(string) you can match the desired string and capture the desired sections from it.



regex = /^(.*) ?((.*) or (.*))$/;
maintext = "Welcome to project,are you (1. Existing user or 2. new user)";
matches = regex.exec(maintext);
text_split1 = matches[1];
text_split2 = matches[2];
text_split3 = matches[3];


Notice that I didn't use matches[0]. That's because this is the full match from the regex, which, in this case, would be the full string you had at first.



Also, notice that the regex may fail with any of the texts inside the parenthesis contain the full word 'or', which is being used to separate the two portions of the string. You must secure this won't happen, or maybe use some special character to separate the two texts instead of the word.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 20 '18 at 16:51









Pedro LimaPedro Lima

361210




361210








  • 1





    Thanks for you help

    – carreankush
    Nov 20 '18 at 16:59











  • You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

    – freedomn-m
    Nov 20 '18 at 19:34











  • @freedomn-m, I used whitespaces.

    – Pedro Lima
    Nov 21 '18 at 1:52











  • s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

    – freedomn-m
    Nov 21 '18 at 8:43











  • I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

    – carreankush
    Nov 21 '18 at 16:12














  • 1





    Thanks for you help

    – carreankush
    Nov 20 '18 at 16:59











  • You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

    – freedomn-m
    Nov 20 '18 at 19:34











  • @freedomn-m, I used whitespaces.

    – Pedro Lima
    Nov 21 '18 at 1:52











  • s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

    – freedomn-m
    Nov 21 '18 at 8:43











  • I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

    – carreankush
    Nov 21 '18 at 16:12








1




1





Thanks for you help

– carreankush
Nov 20 '18 at 16:59





Thanks for you help

– carreankush
Nov 20 '18 at 16:59













You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

– freedomn-m
Nov 20 '18 at 19:34





You could use word-boundary borb or whitespace sors so that it's not picked up inside a word. Still won't fix the issue when there's more than one " or ".

– freedomn-m
Nov 20 '18 at 19:34













@freedomn-m, I used whitespaces.

– Pedro Lima
Nov 21 '18 at 1:52





@freedomn-m, I used whitespaces.

– Pedro Lima
Nov 21 '18 at 1:52













s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

– freedomn-m
Nov 21 '18 at 8:43





s is for whitespace - you've used a space, subtle difference (that likely has no impact on OPs scenario, but just for completeness)

– freedomn-m
Nov 21 '18 at 8:43













I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

– carreankush
Nov 21 '18 at 16:12





I need one more help,some times sentence may like this 'Welcome to project,are you (1. Existing user,2. Test user or 2. new user)' ,I need to get all options in separate console. so how to handle in this case

– carreankush
Nov 21 '18 at 16:12













0














I suggest you take a look at the Split, Substring and IndexOf methods.



For example if you wanted to grab what's inside the parenthesis you could do



var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) )


and then whats outside of it



var outside_split = maintext.substring(0, maintext.indexOf("(") - 1)


or with Split



var split_Text = maintext.split("(") 


Careful with Split here because it'll result in the last parenthesis being included in.



Array [ "Welcome to project,are you ", "1. Existing user or 2. new user)" ]


and from that you can Split the two options with



split_Text[1].split("or")


which results in



Array [ "1. Existing user ", " 2. new user)" ]


and then manipulate the remaining closing parenthesis






share|improve this answer


























  • var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

    – carreankush
    Nov 20 '18 at 16:41











  • Did you read the rest of the post? You can pass "or" to Split

    – Matheus Souza
    Nov 20 '18 at 16:44
















0














I suggest you take a look at the Split, Substring and IndexOf methods.



For example if you wanted to grab what's inside the parenthesis you could do



var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) )


and then whats outside of it



var outside_split = maintext.substring(0, maintext.indexOf("(") - 1)


or with Split



var split_Text = maintext.split("(") 


Careful with Split here because it'll result in the last parenthesis being included in.



Array [ "Welcome to project,are you ", "1. Existing user or 2. new user)" ]


and from that you can Split the two options with



split_Text[1].split("or")


which results in



Array [ "1. Existing user ", " 2. new user)" ]


and then manipulate the remaining closing parenthesis






share|improve this answer


























  • var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

    – carreankush
    Nov 20 '18 at 16:41











  • Did you read the rest of the post? You can pass "or" to Split

    – Matheus Souza
    Nov 20 '18 at 16:44














0












0








0







I suggest you take a look at the Split, Substring and IndexOf methods.



For example if you wanted to grab what's inside the parenthesis you could do



var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) )


and then whats outside of it



var outside_split = maintext.substring(0, maintext.indexOf("(") - 1)


or with Split



var split_Text = maintext.split("(") 


Careful with Split here because it'll result in the last parenthesis being included in.



Array [ "Welcome to project,are you ", "1. Existing user or 2. new user)" ]


and from that you can Split the two options with



split_Text[1].split("or")


which results in



Array [ "1. Existing user ", " 2. new user)" ]


and then manipulate the remaining closing parenthesis






share|improve this answer















I suggest you take a look at the Split, Substring and IndexOf methods.



For example if you wanted to grab what's inside the parenthesis you could do



var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) )


and then whats outside of it



var outside_split = maintext.substring(0, maintext.indexOf("(") - 1)


or with Split



var split_Text = maintext.split("(") 


Careful with Split here because it'll result in the last parenthesis being included in.



Array [ "Welcome to project,are you ", "1. Existing user or 2. new user)" ]


and from that you can Split the two options with



split_Text[1].split("or")


which results in



Array [ "1. Existing user ", " 2. new user)" ]


and then manipulate the remaining closing parenthesis







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 16:33

























answered Nov 20 '18 at 16:28









Matheus SouzaMatheus Souza

648




648













  • var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

    – carreankush
    Nov 20 '18 at 16:41











  • Did you read the rest of the post? You can pass "or" to Split

    – Matheus Souza
    Nov 20 '18 at 16:44



















  • var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

    – carreankush
    Nov 20 '18 at 16:41











  • Did you read the rest of the post? You can pass "or" to Split

    – Matheus Souza
    Nov 20 '18 at 16:44

















var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

– carreankush
Nov 20 '18 at 16:41





var inside_split = maintext.substring(maintext.indexOf( "(" ) + 1, maintext.indexOf( ")" ) ) , I am fine with this but how to separate both the sentence from 'or' again

– carreankush
Nov 20 '18 at 16:41













Did you read the rest of the post? You can pass "or" to Split

– Matheus Souza
Nov 20 '18 at 16:44





Did you read the rest of the post? You can pass "or" to Split

– Matheus Souza
Nov 20 '18 at 16:44


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397205%2fhow-to-extract-text-which-are-outside-and-inside-a-bracket-from-a-sentence-throu%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith