Extract just the path from CSS and exclude anything after the path
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
add a comment |
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
Tryurl([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
add a comment |
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
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
php css regex
edited Nov 26 '18 at 9:35
Micha Wiedenmann
10.4k1364104
10.4k1364104
asked Nov 26 '18 at 9:34
ReadoReado
59641442
59641442
Tryurl([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
add a comment |
Tryurl([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
add a comment |
2 Answers
2
active
oldest
votes
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.
add a comment |
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"
}
>
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 26 '18 at 9:38
Wiktor StribiżewWiktor Stribiżew
315k16133213
315k16133213
add a comment |
add a comment |
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"
}
>
add a comment |
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"
}
>
add a comment |
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"
}
>
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"
}
>
answered Nov 26 '18 at 10:37
Jacob SchneiderJacob Schneider
456316
456316
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%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
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
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