split with regex is not returning correct repsonse [duplicate]












-2
















This question already has an answer here:




  • My regex is matching too much. How do I make it stop?

    5 answers



  • Regular expression to stop at first match [duplicate]

    6 answers



  • Regex to first occurrence only?

    4 answers




I have a string



var str3 = "[a,b,c] there [s,b,c] how are u";


What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/[.*]/);



Any idea how i shud do it?










share|improve this question













marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

    – y2k-shubham
    Nov 20 '18 at 8:45













  • Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

    – Wiktor Stribiżew
    Nov 20 '18 at 8:53


















-2
















This question already has an answer here:




  • My regex is matching too much. How do I make it stop?

    5 answers



  • Regular expression to stop at first match [duplicate]

    6 answers



  • Regex to first occurrence only?

    4 answers




I have a string



var str3 = "[a,b,c] there [s,b,c] how are u";


What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/[.*]/);



Any idea how i shud do it?










share|improve this question













marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

    – y2k-shubham
    Nov 20 '18 at 8:45













  • Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

    – Wiktor Stribiżew
    Nov 20 '18 at 8:53
















-2












-2








-2









This question already has an answer here:




  • My regex is matching too much. How do I make it stop?

    5 answers



  • Regular expression to stop at first match [duplicate]

    6 answers



  • Regex to first occurrence only?

    4 answers




I have a string



var str3 = "[a,b,c] there [s,b,c] how are u";


What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/[.*]/);



Any idea how i shud do it?










share|improve this question















This question already has an answer here:




  • My regex is matching too much. How do I make it stop?

    5 answers



  • Regular expression to stop at first match [duplicate]

    6 answers



  • Regex to first occurrence only?

    4 answers




I have a string



var str3 = "[a,b,c] there [s,b,c] how are u";


What i want is [there, how are u] but I am getting ["", "how are u"], when splitting with str3.split(/[.*]/);



Any idea how i shud do it?





This question already has an answer here:




  • My regex is matching too much. How do I make it stop?

    5 answers



  • Regular expression to stop at first match [duplicate]

    6 answers



  • Regex to first occurrence only?

    4 answers








javascript regex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 8:42









RaazRaaz

7541031




7541031




marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Wiktor Stribiżew javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 8:44


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

    – y2k-shubham
    Nov 20 '18 at 8:45













  • Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

    – Wiktor Stribiżew
    Nov 20 '18 at 8:53





















  • its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

    – y2k-shubham
    Nov 20 '18 at 8:45













  • Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

    – Wiktor Stribiżew
    Nov 20 '18 at 8:53



















its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

– y2k-shubham
Nov 20 '18 at 8:45







its because your regex matches entire section [a,b,c] there [s,b,c] instead of just [a,b,c]

– y2k-shubham
Nov 20 '18 at 8:45















Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

– Wiktor Stribiżew
Nov 20 '18 at 8:53







Your [.*] pattern matches [a,b,c] there [s,b,c] and thus remove this substring and puts an empty string and the rest of the string into an array. Use a non-greedy (also called "lazy") quantifier, *?, instead of *. You may remove empty matches by adding .filter(Boolean) to the split result. Also, to split with whitespace, too, at the same time, use .split(/[.*?]|s+/).filter(Boolean).

– Wiktor Stribiżew
Nov 20 '18 at 8:53














1 Answer
1






active

oldest

votes


















0














You shouldn't be greedy:



str3.split(/[.*?]s*/);


:-)






share|improve this answer


























  • do point me to the duplicate, I will close this @WiktorStribiżew

    – Raaz
    Nov 20 '18 at 8:46






  • 1





    Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

    – MarcoS
    Nov 20 '18 at 8:46











  • Should I better delete my answer?

    – MarcoS
    Nov 20 '18 at 8:52













  • You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

    – Wiktor Stribiżew
    Nov 20 '18 at 9:45




















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You shouldn't be greedy:



str3.split(/[.*?]s*/);


:-)






share|improve this answer


























  • do point me to the duplicate, I will close this @WiktorStribiżew

    – Raaz
    Nov 20 '18 at 8:46






  • 1





    Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

    – MarcoS
    Nov 20 '18 at 8:46











  • Should I better delete my answer?

    – MarcoS
    Nov 20 '18 at 8:52













  • You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

    – Wiktor Stribiżew
    Nov 20 '18 at 9:45


















0














You shouldn't be greedy:



str3.split(/[.*?]s*/);


:-)






share|improve this answer


























  • do point me to the duplicate, I will close this @WiktorStribiżew

    – Raaz
    Nov 20 '18 at 8:46






  • 1





    Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

    – MarcoS
    Nov 20 '18 at 8:46











  • Should I better delete my answer?

    – MarcoS
    Nov 20 '18 at 8:52













  • You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

    – Wiktor Stribiżew
    Nov 20 '18 at 9:45
















0












0








0







You shouldn't be greedy:



str3.split(/[.*?]s*/);


:-)






share|improve this answer















You shouldn't be greedy:



str3.split(/[.*?]s*/);


:-)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 8:45

























answered Nov 20 '18 at 8:44









MarcoSMarcoS

9,5731765135




9,5731765135













  • do point me to the duplicate, I will close this @WiktorStribiżew

    – Raaz
    Nov 20 '18 at 8:46






  • 1





    Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

    – MarcoS
    Nov 20 '18 at 8:46











  • Should I better delete my answer?

    – MarcoS
    Nov 20 '18 at 8:52













  • You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

    – Wiktor Stribiżew
    Nov 20 '18 at 9:45





















  • do point me to the duplicate, I will close this @WiktorStribiżew

    – Raaz
    Nov 20 '18 at 8:46






  • 1





    Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

    – MarcoS
    Nov 20 '18 at 8:46











  • Should I better delete my answer?

    – MarcoS
    Nov 20 '18 at 8:52













  • You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

    – Wiktor Stribiżew
    Nov 20 '18 at 9:45



















do point me to the duplicate, I will close this @WiktorStribiżew

– Raaz
Nov 20 '18 at 8:46





do point me to the duplicate, I will close this @WiktorStribiżew

– Raaz
Nov 20 '18 at 8:46




1




1





Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

– MarcoS
Nov 20 '18 at 8:46





Sorry, didn't know it's a duplicate... What do you suggest to do with duplicates?

– MarcoS
Nov 20 '18 at 8:46













Should I better delete my answer?

– MarcoS
Nov 20 '18 at 8:52







Should I better delete my answer?

– MarcoS
Nov 20 '18 at 8:52















You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

– Wiktor Stribiżew
Nov 20 '18 at 9:45







You cannot delete accepted answers. Once the question is eligible for removal I will cast a delete vote.

– Wiktor Stribiżew
Nov 20 '18 at 9:45







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud