Extract just the path from CSS and exclude anything after the path












2















Using the regex below, I can extract the path from the CSS, but the result also includes the bit after the "#" and "?". Is there any way I can just extract the path



Regex



url([s]?["|']?(.*?)["|']?[s]?)


String



url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix') format('embedded-opentype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff2') format('woff2')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff') format('woff')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.ttf') format('truetype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.svg#OpenSans') format('svg')


Expected



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot


Actual



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix









share|improve this question

























  • Try url([s'"]*K[^'"()?#]+. See live demo here

    – revo
    Nov 26 '18 at 10:54











  • Reado, did you have time to check my approach?

    – Wiktor Stribiżew
    Nov 27 '18 at 7:58


















2















Using the regex below, I can extract the path from the CSS, but the result also includes the bit after the "#" and "?". Is there any way I can just extract the path



Regex



url([s]?["|']?(.*?)["|']?[s]?)


String



url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix') format('embedded-opentype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff2') format('woff2')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff') format('woff')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.ttf') format('truetype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.svg#OpenSans') format('svg')


Expected



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot


Actual



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix









share|improve this question

























  • Try url([s'"]*K[^'"()?#]+. See live demo here

    – revo
    Nov 26 '18 at 10:54











  • Reado, did you have time to check my approach?

    – Wiktor Stribiżew
    Nov 27 '18 at 7:58
















2












2








2








Using the regex below, I can extract the path from the CSS, but the result also includes the bit after the "#" and "?". Is there any way I can just extract the path



Regex



url([s]?["|']?(.*?)["|']?[s]?)


String



url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix') format('embedded-opentype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff2') format('woff2')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff') format('woff')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.ttf') format('truetype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.svg#OpenSans') format('svg')


Expected



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot


Actual



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix









share|improve this question
















Using the regex below, I can extract the path from the CSS, but the result also includes the bit after the "#" and "?". Is there any way I can just extract the path



Regex



url([s]?["|']?(.*?)["|']?[s]?)


String



url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix') format('embedded-opentype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff2') format('woff2')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.woff') format('woff')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.ttf') format('truetype')
url('../fonts/Google_OpenSans/open-sans-v15-latin-italic.svg#OpenSans') format('svg')


Expected



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot


Actual



../fonts/Google_OpenSans/open-sans-v15-latin-italic.eot?#iefix






php css regex






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 '18 at 9:35









Micha Wiedenmann

10.4k1364104




10.4k1364104










asked Nov 26 '18 at 9:34









ReadoReado

59641442




59641442













  • Try url([s'"]*K[^'"()?#]+. See live demo here

    – revo
    Nov 26 '18 at 10:54











  • Reado, did you have time to check my approach?

    – Wiktor Stribiżew
    Nov 27 '18 at 7:58





















  • Try url([s'"]*K[^'"()?#]+. See live demo here

    – revo
    Nov 26 '18 at 10:54











  • Reado, did you have time to check my approach?

    – Wiktor Stribiżew
    Nov 27 '18 at 7:58



















Try url([s'"]*K[^'"()?#]+. See live demo here

– revo
Nov 26 '18 at 10:54





Try url([s'"]*K[^'"()?#]+. See live demo here

– revo
Nov 26 '18 at 10:54













Reado, did you have time to check my approach?

– Wiktor Stribiżew
Nov 27 '18 at 7:58







Reado, did you have time to check my approach?

– Wiktor Stribiżew
Nov 27 '18 at 7:58














2 Answers
2






active

oldest

votes


















1














You may use



url(s*(["']?)([^()?#]*)(?:[#?].*?)?1s*)


See the regex demo. The result will be in Group 2.



Details





  • url( - url( substring


  • s* - 0+ whitespaces


  • (["']?) - Group 1: an optional ' or "


  • ([^()?#]*) - Group 2: any 0+ chars other than ?, #, ) and (


  • (?:[#?].*?)? - an optional substring starting with ? or # and then having any 0+ chars other than line break chars, as few as possible (to make it even more efficient, replace .*? here with [^()]*, cf. with this demo)


  • 1 - same value as captured into Group 1


  • s* - 0+ whitespaces


  • ) - a ) char.






share|improve this answer































    0














    If you're looking for a non-regex based option, I wrote this little function that can parse a URL or path and gives an object of the correct pieces:



    function parseURL (url) {
    url = decodeURI(url);
    let split = {}
    split["?"] = url.split("?")
    split["path"] = split["?"][0]
    split["tmp"] = split["?"][1].split("#")
    split["#"] = split["tmp"][1]
    split["?"] = split["tmp"][0]

    let params = {};

    split["?"].split("&").forEach(i => {
    let tmp = i.split("=");
    params[tmp[0]] = tmp[1];
    })

    return {
    path: encodeURI(split["path"]),
    params,
    fragments: split["#"]
    }
    }


    All you need to do is get the path property from the returned object.



    $ node
    > parseURL("https://example.com/path/to/resource?name=param&purpose=none#extrainfo")
    > {
    path: "https://example.com/path/to/resource",
    params: {
    name: "param",
    purpose: "none"
    },
    fragments: "extrainfo"
    }
    >





    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%2f53478189%2fextract-just-the-path-from-css-and-exclude-anything-after-the-path%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You may use



      url(s*(["']?)([^()?#]*)(?:[#?].*?)?1s*)


      See the regex demo. The result will be in Group 2.



      Details





      • url( - url( substring


      • s* - 0+ whitespaces


      • (["']?) - Group 1: an optional ' or "


      • ([^()?#]*) - Group 2: any 0+ chars other than ?, #, ) and (


      • (?:[#?].*?)? - an optional substring starting with ? or # and then having any 0+ chars other than line break chars, as few as possible (to make it even more efficient, replace .*? here with [^()]*, cf. with this demo)


      • 1 - same value as captured into Group 1


      • s* - 0+ whitespaces


      • ) - a ) char.






      share|improve this answer




























        1














        You may use



        url(s*(["']?)([^()?#]*)(?:[#?].*?)?1s*)


        See the regex demo. The result will be in Group 2.



        Details





        • url( - url( substring


        • s* - 0+ whitespaces


        • (["']?) - Group 1: an optional ' or "


        • ([^()?#]*) - Group 2: any 0+ chars other than ?, #, ) and (


        • (?:[#?].*?)? - an optional substring starting with ? or # and then having any 0+ chars other than line break chars, as few as possible (to make it even more efficient, replace .*? here with [^()]*, cf. with this demo)


        • 1 - same value as captured into Group 1


        • s* - 0+ whitespaces


        • ) - a ) char.






        share|improve this answer


























          1












          1








          1







          You may use



          url(s*(["']?)([^()?#]*)(?:[#?].*?)?1s*)


          See the regex demo. The result will be in Group 2.



          Details





          • url( - url( substring


          • s* - 0+ whitespaces


          • (["']?) - Group 1: an optional ' or "


          • ([^()?#]*) - Group 2: any 0+ chars other than ?, #, ) and (


          • (?:[#?].*?)? - an optional substring starting with ? or # and then having any 0+ chars other than line break chars, as few as possible (to make it even more efficient, replace .*? here with [^()]*, cf. with this demo)


          • 1 - same value as captured into Group 1


          • s* - 0+ whitespaces


          • ) - a ) char.






          share|improve this answer













          You may use



          url(s*(["']?)([^()?#]*)(?:[#?].*?)?1s*)


          See the regex demo. The result will be in Group 2.



          Details





          • url( - url( substring


          • s* - 0+ whitespaces


          • (["']?) - Group 1: an optional ' or "


          • ([^()?#]*) - Group 2: any 0+ chars other than ?, #, ) and (


          • (?:[#?].*?)? - an optional substring starting with ? or # and then having any 0+ chars other than line break chars, as few as possible (to make it even more efficient, replace .*? here with [^()]*, cf. with this demo)


          • 1 - same value as captured into Group 1


          • s* - 0+ whitespaces


          • ) - a ) char.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 9:38









          Wiktor StribiżewWiktor Stribiżew

          315k16133213




          315k16133213

























              0














              If you're looking for a non-regex based option, I wrote this little function that can parse a URL or path and gives an object of the correct pieces:



              function parseURL (url) {
              url = decodeURI(url);
              let split = {}
              split["?"] = url.split("?")
              split["path"] = split["?"][0]
              split["tmp"] = split["?"][1].split("#")
              split["#"] = split["tmp"][1]
              split["?"] = split["tmp"][0]

              let params = {};

              split["?"].split("&").forEach(i => {
              let tmp = i.split("=");
              params[tmp[0]] = tmp[1];
              })

              return {
              path: encodeURI(split["path"]),
              params,
              fragments: split["#"]
              }
              }


              All you need to do is get the path property from the returned object.



              $ node
              > parseURL("https://example.com/path/to/resource?name=param&purpose=none#extrainfo")
              > {
              path: "https://example.com/path/to/resource",
              params: {
              name: "param",
              purpose: "none"
              },
              fragments: "extrainfo"
              }
              >





              share|improve this answer




























                0














                If you're looking for a non-regex based option, I wrote this little function that can parse a URL or path and gives an object of the correct pieces:



                function parseURL (url) {
                url = decodeURI(url);
                let split = {}
                split["?"] = url.split("?")
                split["path"] = split["?"][0]
                split["tmp"] = split["?"][1].split("#")
                split["#"] = split["tmp"][1]
                split["?"] = split["tmp"][0]

                let params = {};

                split["?"].split("&").forEach(i => {
                let tmp = i.split("=");
                params[tmp[0]] = tmp[1];
                })

                return {
                path: encodeURI(split["path"]),
                params,
                fragments: split["#"]
                }
                }


                All you need to do is get the path property from the returned object.



                $ node
                > parseURL("https://example.com/path/to/resource?name=param&purpose=none#extrainfo")
                > {
                path: "https://example.com/path/to/resource",
                params: {
                name: "param",
                purpose: "none"
                },
                fragments: "extrainfo"
                }
                >





                share|improve this answer


























                  0












                  0








                  0







                  If you're looking for a non-regex based option, I wrote this little function that can parse a URL or path and gives an object of the correct pieces:



                  function parseURL (url) {
                  url = decodeURI(url);
                  let split = {}
                  split["?"] = url.split("?")
                  split["path"] = split["?"][0]
                  split["tmp"] = split["?"][1].split("#")
                  split["#"] = split["tmp"][1]
                  split["?"] = split["tmp"][0]

                  let params = {};

                  split["?"].split("&").forEach(i => {
                  let tmp = i.split("=");
                  params[tmp[0]] = tmp[1];
                  })

                  return {
                  path: encodeURI(split["path"]),
                  params,
                  fragments: split["#"]
                  }
                  }


                  All you need to do is get the path property from the returned object.



                  $ node
                  > parseURL("https://example.com/path/to/resource?name=param&purpose=none#extrainfo")
                  > {
                  path: "https://example.com/path/to/resource",
                  params: {
                  name: "param",
                  purpose: "none"
                  },
                  fragments: "extrainfo"
                  }
                  >





                  share|improve this answer













                  If you're looking for a non-regex based option, I wrote this little function that can parse a URL or path and gives an object of the correct pieces:



                  function parseURL (url) {
                  url = decodeURI(url);
                  let split = {}
                  split["?"] = url.split("?")
                  split["path"] = split["?"][0]
                  split["tmp"] = split["?"][1].split("#")
                  split["#"] = split["tmp"][1]
                  split["?"] = split["tmp"][0]

                  let params = {};

                  split["?"].split("&").forEach(i => {
                  let tmp = i.split("=");
                  params[tmp[0]] = tmp[1];
                  })

                  return {
                  path: encodeURI(split["path"]),
                  params,
                  fragments: split["#"]
                  }
                  }


                  All you need to do is get the path property from the returned object.



                  $ node
                  > parseURL("https://example.com/path/to/resource?name=param&purpose=none#extrainfo")
                  > {
                  path: "https://example.com/path/to/resource",
                  params: {
                  name: "param",
                  purpose: "none"
                  },
                  fragments: "extrainfo"
                  }
                  >






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 26 '18 at 10:37









                  Jacob SchneiderJacob Schneider

                  456316




                  456316






























                      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%2f53478189%2fextract-just-the-path-from-css-and-exclude-anything-after-the-path%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

                      Lallio

                      Futebolista

                      Jornalista