split with regex is not returning correct repsonse [duplicate]
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?
javascript regex
marked as duplicate by Wiktor Stribiżew
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.
add a comment |
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?
javascript regex
marked as duplicate by Wiktor Stribiżew
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
add a comment |
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?
javascript regex
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
javascript regex
asked Nov 20 '18 at 8:42
RaazRaaz
7541031
7541031
marked as duplicate by Wiktor Stribiżew
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
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
You shouldn't be greedy:
str3.split(/[.*?]s*/);
:-)
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You shouldn't be greedy:
str3.split(/[.*?]s*/);
:-)
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
add a comment |
You shouldn't be greedy:
str3.split(/[.*?]s*/);
:-)
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
add a comment |
You shouldn't be greedy:
str3.split(/[.*?]s*/);
:-)
You shouldn't be greedy:
str3.split(/[.*?]s*/);
:-)
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
add a comment |
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
add a comment |
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