Javascript split sentence only by first concurrence












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










share|improve this question


















  • 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
















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










share|improve this question


















  • 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














-4












-4








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










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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














  • 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








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












3 Answers
3






active

oldest

votes


















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








share|improve this answer


























  • 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








  • 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



















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





share|improve this answer

































    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', '').split('') )








    share|improve this answer























      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%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









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








      share|improve this answer


























      • 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








      • 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
















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








      share|improve this answer


























      • 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








      • 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














      3












      3








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








      share|improve this answer















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






      share|improve this answer














      share|improve this answer



      share|improve this answer








      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 of ell?

        – 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











      • @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













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





      share|improve this answer






























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





        share|improve this answer




























          2












          2








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





          share|improve this answer















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






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 12:31

























          answered Nov 20 '18 at 12:26









          Teun van der WijstTeun van der Wijst

          570315




          570315























              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', '').split('') )








              share|improve this answer




























                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', '').split('') )








                share|improve this answer


























                  1












                  1








                  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', '').split('') )








                  share|improve this answer













                  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('') )






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 13:14









                  SlaiSlai

                  15.3k32235




                  15.3k32235






























                      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%2f53392920%2fjavascript-split-sentence-only-by-first-concurrence%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

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

                      How to fix TextFormField cause rebuild widget in Flutter