-4

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"]

gizmo
  • 191
  • 1
  • 11

3 Answers3

3

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));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 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
  • @sjahan I wish, but regex is not my pal at all. I tried similar solutions , but couldnt find a way – gizmo Nov 20 '18 at 12:31
  • 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
  • I think it doesnt work if signs are used, like `+`. Could you confirm? `var string = '+ell'` and `console.log("h+ello my dear h+ello blah blah".match(regexp).slice(1));` – gizmo Nov 20 '18 at 12:50
  • 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
  • Thanks. But if I dont know will there be a `+` or no, adding slash won't make any errors? – gizmo Nov 20 '18 at 12:53
  • it makes, because some other characters, like linefeed or new line needs a backslash in front of it. – Nina Scholz Nov 20 '18 at 12:55
  • @AnonymousSB, the double backslash quotes the backslash, not the quantifier. – Nina Scholz Nov 20 '18 at 12:56
  • Ok, so I just check with `startsWith('+')`, but this still seems fragile. Thanks anyway~ – gizmo Nov 20 '18 at 12:56
  • @NinaScholz That’s not the case, try it for yourself with just one backslash, I did. – AnonymousSB Nov 20 '18 at 12:57
  • @gizmo you may be better off with Teun’s solution of just using slice to avoid the headache of using user input inside a regular expression. – AnonymousSB Nov 20 '18 at 13:00
2

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"]
Teun van der Wijst
  • 939
  • 1
  • 7
  • 21
1

Replace with something that will not be in the string, and split by it :

console.log( "hello my dear hello blah blah".replace('ello', '\0').split('\0') )
Slai
  • 22,144
  • 5
  • 45
  • 53