Javascript split sentence only by first concurrence
I need to split a sentence, but only by a first concurrence. For ex:
"hello my dear hello blah blah".split('ello')
Expected result:
["h", "o my dear hello blah blah"]
javascript
add a comment |
I need to split a sentence, but only by a first concurrence. For ex:
"hello my dear hello blah blah".split('ello')
Expected result:
["h", "o my dear hello blah blah"]
javascript
1
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
2
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
build your solution arroundString.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
1
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32
add a comment |
I need to split a sentence, but only by a first concurrence. For ex:
"hello my dear hello blah blah".split('ello')
Expected result:
["h", "o my dear hello blah blah"]
javascript
I need to split a sentence, but only by a first concurrence. For ex:
"hello my dear hello blah blah".split('ello')
Expected result:
["h", "o my dear hello blah blah"]
javascript
javascript
asked Nov 20 '18 at 12:23
gizmogizmo
666
666
1
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
2
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
build your solution arroundString.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
1
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32
add a comment |
1
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
2
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
build your solution arroundString.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
1
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32
1
1
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
2
2
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
build your solution arround
String.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
build your solution arround
String.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
1
1
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32
add a comment |
3 Answers
3
active
oldest
votes
You could match ell
with a non greedy search and take only the groups left and right from it.
String#split
does not work here, because it works global for the string.
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
Using a variable (mind special characters!) and a RegExp
constructor.
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead ofell
?
– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
that's wha i wrote "mind special characters!".+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like+
.
– Nina Scholz
Nov 20 '18 at 12:52
1
That's correct, because your search is becoming a part of the regular expression, and+
is a regular expression, so you'd have to escape it like so\+ell
– AnonymousSB
Nov 20 '18 at 12:52
|
show 9 more comments
You can look for the first index of the occurance of your search string, then split the original on that index, like so:
const sentence = "hello my dear hello blah blah"
const searchValue = 'ell';
const index = sentence.indexOf(searchValue);
const result=
result.push(sentence.slice(0, index));
result.push(sentence.slice(index+ searchValue.length));
// result = ["h", "o my dear hello blah blah"]
add a comment |
Replace with something that will not be in the string, and split by it :
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
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%2f53392920%2fjavascript-split-sentence-only-by-first-concurrence%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could match ell
with a non greedy search and take only the groups left and right from it.
String#split
does not work here, because it works global for the string.
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
Using a variable (mind special characters!) and a RegExp
constructor.
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead ofell
?
– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
that's wha i wrote "mind special characters!".+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like+
.
– Nina Scholz
Nov 20 '18 at 12:52
1
That's correct, because your search is becoming a part of the regular expression, and+
is a regular expression, so you'd have to escape it like so\+ell
– AnonymousSB
Nov 20 '18 at 12:52
|
show 9 more comments
You could match ell
with a non greedy search and take only the groups left and right from it.
String#split
does not work here, because it works global for the string.
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
Using a variable (mind special characters!) and a RegExp
constructor.
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead ofell
?
– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
that's wha i wrote "mind special characters!".+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like+
.
– Nina Scholz
Nov 20 '18 at 12:52
1
That's correct, because your search is becoming a part of the regular expression, and+
is a regular expression, so you'd have to escape it like so\+ell
– AnonymousSB
Nov 20 '18 at 12:52
|
show 9 more comments
You could match ell
with a non greedy search and take only the groups left and right from it.
String#split
does not work here, because it works global for the string.
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
Using a variable (mind special characters!) and a RegExp
constructor.
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
You could match ell
with a non greedy search and take only the groups left and right from it.
String#split
does not work here, because it works global for the string.
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
Using a variable (mind special characters!) and a RegExp
constructor.
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
console.log("hello my dear hello blah blah".match(/^(.*?)ell(.*)$/).slice(1));
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
var string = 'ell',
regexp = new RegExp('^(.*?)' + string + '(.*)$');
console.log("hello my dear hello blah blah".match(regexp).slice(1));
edited Nov 20 '18 at 12:31
answered Nov 20 '18 at 12:28


Nina ScholzNina Scholz
180k1493160
180k1493160
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead ofell
?
– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
that's wha i wrote "mind special characters!".+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like+
.
– Nina Scholz
Nov 20 '18 at 12:52
1
That's correct, because your search is becoming a part of the regular expression, and+
is a regular expression, so you'd have to escape it like so\+ell
– AnonymousSB
Nov 20 '18 at 12:52
|
show 9 more comments
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead ofell
?
– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
that's wha i wrote "mind special characters!".+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like+
.
– Nina Scholz
Nov 20 '18 at 12:52
1
That's correct, because your search is becoming a part of the regular expression, and+
is a regular expression, so you'd have to escape it like so\+ell
– AnonymousSB
Nov 20 '18 at 12:52
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead of
ell
?– gizmo
Nov 20 '18 at 12:29
Thats exactly I needed. Could you please tell one more thing - how to pass a variable instead of
ell
?– gizmo
Nov 20 '18 at 12:29
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
@gizmo are you kidding? :/
– sjahan
Nov 20 '18 at 12:30
1
1
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
Oh I must use a RegExp constructor... I thought its possible just with useual expression (if that supposed to called expression)
– gizmo
Nov 20 '18 at 12:33
1
1
that's wha i wrote "mind special characters!".
+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like +
.– Nina Scholz
Nov 20 '18 at 12:52
that's wha i wrote "mind special characters!".
+
is a quantifier in regular expression. to quote it, you need to add a backslash in front of it, like +
.– Nina Scholz
Nov 20 '18 at 12:52
1
1
That's correct, because your search is becoming a part of the regular expression, and
+
is a regular expression, so you'd have to escape it like so \+ell
– AnonymousSB
Nov 20 '18 at 12:52
That's correct, because your search is becoming a part of the regular expression, and
+
is a regular expression, so you'd have to escape it like so \+ell
– AnonymousSB
Nov 20 '18 at 12:52
|
show 9 more comments
You can look for the first index of the occurance of your search string, then split the original on that index, like so:
const sentence = "hello my dear hello blah blah"
const searchValue = 'ell';
const index = sentence.indexOf(searchValue);
const result=
result.push(sentence.slice(0, index));
result.push(sentence.slice(index+ searchValue.length));
// result = ["h", "o my dear hello blah blah"]
add a comment |
You can look for the first index of the occurance of your search string, then split the original on that index, like so:
const sentence = "hello my dear hello blah blah"
const searchValue = 'ell';
const index = sentence.indexOf(searchValue);
const result=
result.push(sentence.slice(0, index));
result.push(sentence.slice(index+ searchValue.length));
// result = ["h", "o my dear hello blah blah"]
add a comment |
You can look for the first index of the occurance of your search string, then split the original on that index, like so:
const sentence = "hello my dear hello blah blah"
const searchValue = 'ell';
const index = sentence.indexOf(searchValue);
const result=
result.push(sentence.slice(0, index));
result.push(sentence.slice(index+ searchValue.length));
// result = ["h", "o my dear hello blah blah"]
You can look for the first index of the occurance of your search string, then split the original on that index, like so:
const sentence = "hello my dear hello blah blah"
const searchValue = 'ell';
const index = sentence.indexOf(searchValue);
const result=
result.push(sentence.slice(0, index));
result.push(sentence.slice(index+ searchValue.length));
// result = ["h", "o my dear hello blah blah"]
edited Nov 20 '18 at 12:31
answered Nov 20 '18 at 12:26
Teun van der WijstTeun van der Wijst
570315
570315
add a comment |
add a comment |
Replace with something that will not be in the string, and split by it :
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
add a comment |
Replace with something that will not be in the string, and split by it :
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
add a comment |
Replace with something that will not be in the string, and split by it :
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
Replace with something that will not be in the string, and split by it :
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
console.log( "hello my dear hello blah blah".replace('ello', '').split('') )
answered Nov 20 '18 at 13:14


SlaiSlai
15.3k32235
15.3k32235
add a comment |
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%2f53392920%2fjavascript-split-sentence-only-by-first-concurrence%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
1
Where's the code?
– squeekyDave
Nov 20 '18 at 12:24
2
Possible duplicate of Split string once in javascript?
– Luca Kiebel
Nov 20 '18 at 12:26
build your solution arround
String.prototype.indexOf
– mistahenry
Nov 20 '18 at 12:26
1
@Ivar. It was by mistake. stackoverflow.com/questions/2878703/…
– Karan
Nov 20 '18 at 12:32