JavaScript Regex get multiple single quotation marks inside single quotation marks in a SQL insert












0















Problem



I am doing a database migration and I came across the following senario, I have single quotation mark inside the single quotation marks of the insert and after several attempts to fix I reached the conclusion that I would need to use regex and I arrived to the point where I could replace the single quotation mark in the center of the text, I now have a problem when it appears at the beginning. my regex has the first two single quotation mark but my goal is the last two.



SQL Insert:



(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

- Lorem ipsum dolor '' sit amet - ''''

Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.

', null, null);


My Regex(js):



((["'])(?:(?=(''?))2)*?)1(?!,)


My goal after regexp and make a find and replace is to have this as a result:



(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

- Lorem ipsum dolor '' sit amet - ''''

Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
Integer sit amet lacus ornare massa feugiat tempus.

', null, null);


I am using this site to test: https://regex101.com/ (with javascript selected)










share|improve this question



























    0















    Problem



    I am doing a database migration and I came across the following senario, I have single quotation mark inside the single quotation marks of the insert and after several attempts to fix I reached the conclusion that I would need to use regex and I arrived to the point where I could replace the single quotation mark in the center of the text, I now have a problem when it appears at the beginning. my regex has the first two single quotation mark but my goal is the last two.



    SQL Insert:



    (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

    - Lorem ipsum dolor '' sit amet - ''''

    Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
    Integer sit amet lacus ornare massa feugiat tempus.

    ', null, null);


    My Regex(js):



    ((["'])(?:(?=(''?))2)*?)1(?!,)


    My goal after regexp and make a find and replace is to have this as a result:



    (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

    - Lorem ipsum dolor '' sit amet - ''''

    Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
    Integer sit amet lacus ornare massa feugiat tempus.

    ', null, null);


    I am using this site to test: https://regex101.com/ (with javascript selected)










    share|improve this question

























      0












      0








      0








      Problem



      I am doing a database migration and I came across the following senario, I have single quotation mark inside the single quotation marks of the insert and after several attempts to fix I reached the conclusion that I would need to use regex and I arrived to the point where I could replace the single quotation mark in the center of the text, I now have a problem when it appears at the beginning. my regex has the first two single quotation mark but my goal is the last two.



      SQL Insert:



      (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

      - Lorem ipsum dolor '' sit amet - ''''

      Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
      Integer sit amet lacus ornare massa feugiat tempus.

      ', null, null);


      My Regex(js):



      ((["'])(?:(?=(''?))2)*?)1(?!,)


      My goal after regexp and make a find and replace is to have this as a result:



      (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

      - Lorem ipsum dolor '' sit amet - ''''

      Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
      Integer sit amet lacus ornare massa feugiat tempus.

      ', null, null);


      I am using this site to test: https://regex101.com/ (with javascript selected)










      share|improve this question














      Problem



      I am doing a database migration and I came across the following senario, I have single quotation mark inside the single quotation marks of the insert and after several attempts to fix I reached the conclusion that I would need to use regex and I arrived to the point where I could replace the single quotation mark in the center of the text, I now have a problem when it appears at the beginning. my regex has the first two single quotation mark but my goal is the last two.



      SQL Insert:



      (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

      - Lorem ipsum dolor '' sit amet - ''''

      Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
      Integer sit amet lacus ornare massa feugiat tempus.

      ', null, null);


      My Regex(js):



      ((["'])(?:(?=(''?))2)*?)1(?!,)


      My goal after regexp and make a find and replace is to have this as a result:



      (1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
      Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

      - Lorem ipsum dolor '' sit amet - ''''

      Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
      Integer sit amet lacus ornare massa feugiat tempus.

      ', null, null);


      I am using this site to test: https://regex101.com/ (with javascript selected)







      javascript sql regex migration






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 0:25









      FilipeFilipe

      356




      356
























          1 Answer
          1






          active

          oldest

          votes


















          1














          One option would be to match a ' or " delimiter, then lazy-repeat until you match that delimiter again followed by a comma (or a ), in case the item is the last in the list). Then, use a replacer function to replace everything inside the delimiters with 's escaped:






          const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
          Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

          - Lorem ipsum dolor '' sit amet - ''''

          Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
          Integer sit amet lacus ornare massa feugiat tempus.

          ', null, null);`;


          const output = input.replace(
          /(["'])([sS]*?)1(?=[,)])/g,
          (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
          );
          console.log(output);





          https://regex101.com/r/RRmq8g/1



          If you want to escape "s like that when the delimiters are ", then construct the replacer regex from the delim:



          const output = input.replace(
          /(["'])([sS]*?)1(?=[,)])/g,
          (_, delim, content) => {
          const pattern = new RegExp(delim, 'g')
          return delim + content.replace(pattern, '\' + delim) + delim
          }
          );





          const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

          Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
          Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

          - Lorem ipsum dolor '' sit amet - ''''

          Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
          Integer sit amet lacus ornare massa feugiat tempus.

          ', null, null);`;


          const output = input.replace(
          /(["'])([sS]*?)1(?=[,)])/g,
          (_, delim, content) => {
          const pattern = new RegExp(delim, 'g')
          return delim + content.replace(pattern, '\' + delim) + delim
          }
          );
          console.log(output);





          /(["'])([sS]*?)1(?=[,)])/g means:





          • (["']) Match and capture a delimiter


          • ([sS]*?) - Repeat any character, until you get to:


          • 1 - The delimiter, followed by


          • (?=[,)]) - either a comma, or a )






          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%2f53384535%2fjavascript-regex-get-multiple-single-quotation-marks-inside-single-quotation-mar%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            One option would be to match a ' or " delimiter, then lazy-repeat until you match that delimiter again followed by a comma (or a ), in case the item is the last in the list). Then, use a replacer function to replace everything inside the delimiters with 's escaped:






            const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
            Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

            - Lorem ipsum dolor '' sit amet - ''''

            Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
            Integer sit amet lacus ornare massa feugiat tempus.

            ', null, null);`;


            const output = input.replace(
            /(["'])([sS]*?)1(?=[,)])/g,
            (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
            );
            console.log(output);





            https://regex101.com/r/RRmq8g/1



            If you want to escape "s like that when the delimiters are ", then construct the replacer regex from the delim:



            const output = input.replace(
            /(["'])([sS]*?)1(?=[,)])/g,
            (_, delim, content) => {
            const pattern = new RegExp(delim, 'g')
            return delim + content.replace(pattern, '\' + delim) + delim
            }
            );





            const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

            Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
            Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

            - Lorem ipsum dolor '' sit amet - ''''

            Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
            Integer sit amet lacus ornare massa feugiat tempus.

            ', null, null);`;


            const output = input.replace(
            /(["'])([sS]*?)1(?=[,)])/g,
            (_, delim, content) => {
            const pattern = new RegExp(delim, 'g')
            return delim + content.replace(pattern, '\' + delim) + delim
            }
            );
            console.log(output);





            /(["'])([sS]*?)1(?=[,)])/g means:





            • (["']) Match and capture a delimiter


            • ([sS]*?) - Repeat any character, until you get to:


            • 1 - The delimiter, followed by


            • (?=[,)]) - either a comma, or a )






            share|improve this answer






























              1














              One option would be to match a ' or " delimiter, then lazy-repeat until you match that delimiter again followed by a comma (or a ), in case the item is the last in the list). Then, use a replacer function to replace everything inside the delimiters with 's escaped:






              const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
              Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

              - Lorem ipsum dolor '' sit amet - ''''

              Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
              Integer sit amet lacus ornare massa feugiat tempus.

              ', null, null);`;


              const output = input.replace(
              /(["'])([sS]*?)1(?=[,)])/g,
              (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
              );
              console.log(output);





              https://regex101.com/r/RRmq8g/1



              If you want to escape "s like that when the delimiters are ", then construct the replacer regex from the delim:



              const output = input.replace(
              /(["'])([sS]*?)1(?=[,)])/g,
              (_, delim, content) => {
              const pattern = new RegExp(delim, 'g')
              return delim + content.replace(pattern, '\' + delim) + delim
              }
              );





              const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
              Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

              - Lorem ipsum dolor '' sit amet - ''''

              Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
              Integer sit amet lacus ornare massa feugiat tempus.

              ', null, null);`;


              const output = input.replace(
              /(["'])([sS]*?)1(?=[,)])/g,
              (_, delim, content) => {
              const pattern = new RegExp(delim, 'g')
              return delim + content.replace(pattern, '\' + delim) + delim
              }
              );
              console.log(output);





              /(["'])([sS]*?)1(?=[,)])/g means:





              • (["']) Match and capture a delimiter


              • ([sS]*?) - Repeat any character, until you get to:


              • 1 - The delimiter, followed by


              • (?=[,)]) - either a comma, or a )






              share|improve this answer




























                1












                1








                1







                One option would be to match a ' or " delimiter, then lazy-repeat until you match that delimiter again followed by a comma (or a ), in case the item is the last in the list). Then, use a replacer function to replace everything inside the delimiters with 's escaped:






                const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
                );
                console.log(output);





                https://regex101.com/r/RRmq8g/1



                If you want to escape "s like that when the delimiters are ", then construct the replacer regex from the delim:



                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );





                const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );
                console.log(output);





                /(["'])([sS]*?)1(?=[,)])/g means:





                • (["']) Match and capture a delimiter


                • ([sS]*?) - Repeat any character, until you get to:


                • 1 - The delimiter, followed by


                • (?=[,)]) - either a comma, or a )






                share|improve this answer















                One option would be to match a ' or " delimiter, then lazy-repeat until you match that delimiter again followed by a comma (or a ), in case the item is the last in the list). Then, use a replacer function to replace everything inside the delimiters with 's escaped:






                const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
                );
                console.log(output);





                https://regex101.com/r/RRmq8g/1



                If you want to escape "s like that when the delimiters are ", then construct the replacer regex from the delim:



                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );





                const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );
                console.log(output);





                /(["'])([sS]*?)1(?=[,)])/g means:





                • (["']) Match and capture a delimiter


                • ([sS]*?) - Repeat any character, until you get to:


                • 1 - The delimiter, followed by


                • (?=[,)]) - either a comma, or a )






                const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
                );
                console.log(output);





                const input = `(1, '22', '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => delim + content.replace(/'/g, "\'") + delim
                );
                console.log(output);





                const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );
                console.log(output);





                const input = `(1, "22 foo bar "double quotes" here ", '2006-11-10', '3', 'field1', 'field2', '2006-12-12', '11:20:06', '''field3', '

                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus ex ipsum, luctus id dignissim tempor, volutpat eget sapien. Cras accumsan molestie iaculis. Mauris vehicula a nunc vel tincidunt.
                Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

                - Lorem ipsum dolor '' sit amet - ''''

                Aliquam vestibulum tincidunt odio, id varius sapien lacinia ac. Phasellus ullamcorper ante ut lacus dictum commodo.
                Integer sit amet lacus ornare massa feugiat tempus.

                ', null, null);`;


                const output = input.replace(
                /(["'])([sS]*?)1(?=[,)])/g,
                (_, delim, content) => {
                const pattern = new RegExp(delim, 'g')
                return delim + content.replace(pattern, '\' + delim) + delim
                }
                );
                console.log(output);






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 20 '18 at 0:44

























                answered Nov 20 '18 at 0:39









                CertainPerformanceCertainPerformance

                86.6k154774




                86.6k154774
































                    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%2f53384535%2fjavascript-regex-get-multiple-single-quotation-marks-inside-single-quotation-mar%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

                    Guess what letter conforming each word

                    Port of Spain

                    Run scheduled task as local user group (not BUILTIN)