I'm trying to match font files that are contained in CSS files.
The font files can have the following extensions :
And following format :
- url("../../../../fonts/font_awesome/fontawesome-webfont.ttf?v=4.6.3")
- url("../../../../fonts/font_awesome/fontawesome-webfont.svg?v=4.6.3#fontawesomeregular")
In this case the match should be :
../../../../fonts/font_awesome/fontawesome-webfont.ttf?v=4.6.3
../../../../fonts/font_awesome/fontawesome-webfont.svg?v=4.6.3#fontawesomeregular
I have already made a regex that matches them successfully, however on using an online service, the regex returned to me a warning saying that it has a catastrophic backtracking, so I'm asking how can this Regex be better :
url\s*\(\s*\S(.+?\.(woff|eot|woff2|ttf|svg)\s*)\??\S+?\s?\)
Regex in action with the backtracking warning : https://regex101.com/r/KWsh1X/1
This will do the job:
url\("([^\)]+?\.(woff|eot|woff2|ttf|svg)[^"]*)"
Here you can play around: https://regex101.com/r/KWsh1X/2
Look like it's late but maybe will be helpfull too
url\(['"]([^"']+(woff|eot|woff2|ttf|svg)[^"']+)
\W+url\W+"([^"]+\.(woff|eot|woff2|ttf|svg)[^"]+)"\W+
You don't have to do as many steps if you don't include the file formats.
/url\("(.*?)"\)/
Related
I'm very new on this and I isn't a pro on this issues!
I'm just here for request for your help because I know this community is the best for this problems!
Actually I'm using a software called "Bulk Rename Utility", it works for bulk rename files with a very useful interface, one of them is the RegEx option where you can rename files thanks to the regular expresion lenguage.
I was wonder if I can convert this code in a JavaScript code, this is because I need to modify various parameters and it could be better if I can use a JavaScript code.
As I told you, I'm very new on this. After checking some videos I learned how to use the RegEx option in this program and the codes I got was:
In the "Match" field of the program I write: (.*) 10-1[A-Z] (.*).
In the "Replace" filed of the program I write: \1_REF_\2
A then, it do the magic!
As I said, I want to know how I can convert this code to a JavaScript code because I have a lot of this codes and I want just one whole code to make a bulk process.
Thanks you so much!
Like this?
const str = 'abc 10-1G def';
const outputStr = str.replace(/(.*) 10-1[A-Z] (.*)/, '$1REF$2');
console.log(outputStr);
You just need to put $1/$2 in place of the captured groups you want in the replaced string.
To insert code, you can put the code between backticks (`) and you'll be able to type *s normally.
I have a large project that has many *.html files and many *.tpl.html files.
I want to use a regular expression that allows me to differentiate between these two for my Webpack config.
I have tried using laziness to achieve this, like .*?\.html but this also matches *.tpl.html. https://regex101.com/r/a0fl4H/1
How can this be achieved?
Try this:
^(?!.*\.tpl).+\.html$
Demo:
https://regex101.com/r/a0fl4H/8
For regex, this should do it;
/.*?[^.tpl]\.html/
Working example
Edit: This first solution needs improvement. As mentioned in the comments, this will provide false positives for test.t.html - as it matches any of the given characters (.tpl).
This is a working version using;
^(?!.*\.tpl).*.html
bar.html // matches
bar.tpl.html // doesn't match
test.t.html // matches
test.p.html // matches
test.z.html // matches
Been struggling for the last hour to try and get this regexp to work but cannot seem to crack it.
It must be a regexp and I cannot use split etc as it is part of a bigger regexp that searches for numerous other strings using .test().
(public\/css.*[!\/]?)
public/css/somefile.css
public/css/somepath/somefile.css
public/css/somepath/anotherpath/somefile.css
Here I am trying to look for path starting with public/css followed by any character except for another forward slash.
so "public/css/somefile.css" should match but the other 2 should not.
A better solution may be to somehow specify the number of levels to match after the prefix using something like
(public\/css\/{1,2}.*)
but I can't seem to figure that out either, some help with this would be appreciated.
edit
No idea why this question has been marked down twice, I have clearly stated the requirement with sample code and test cases and also attempted to solve the issue, why is it being marked down ?
You can use this regex:
/^(public\/css\/[^\/]*?)$/gm
^ : Starts with
[^/] : Not /
*?: Any Characters
$: Ends with
g: Global Flag
m: Multi-line Flag
Something like this?
/public\/css\/[^\/]+$/
This will match
public/css/[Any characters except for /]$
$ is matching the end of the string in regex.
I'm trying to construct URL RegExp. The base expression looks like:
/^(((http(?:s)?\:\/\/)|www\.)[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$/
It looks good for me, because matches these:
http://gmail.com
http://www.gmail.com
www.gmail.com
But I wold like to modify it to match this:
gmail.com
I will appreciate any help.
just add a ? to make www optional, then it will match gmail.com also
use this :
^(((http(?:s)?\:\/\/)|www\.)?[a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$
or if you want to match only gmail.com and not http://gmail.com in that case use this :
^([a-zA-Z0-9\-]+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$
please note , this will match anu string which has dots and alphabets in it.
IMO it will be better off using a regex like this :
^(http:\/\/|www\.)?[\w\.]+\.(com|net|co\.cc|co\.in)$
you can modify it according to your needs .
check out a demo here and play around with the regex :
http://regex101.com/r/tS4aB3
The easiest way is to treat 'www' as just another subdomain (because that's all it is).
So:
/^(((http(?:s)?\:\/\/))?([a-zA-Z0-9\-]+\.?)+(?:\.[a-zA-Z0-9\-]+)*\.[a-zA-Z]{2,6}(?:\/?|(?:\/[\w\-]+)*)(?:\/?|\/\w+((\.[a-zA-Z]{2,4})?)(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w]+\=[\w\-]+)*)$/
Edit: as a side note, the tld (i.e. the ".com" part) is... quite complicated these days. There are a lot of them, and they may not fit easily in 2-6 chars.
For a web app I'm trying to come up with a javascript regex that matches anything not ending in .json. Sounds simple but I'm finding it pretty damn hard.
I first wanted to do it like this: ^.*(?!\.json$), but that obviously didn't work as it simply matches the entire string. Then I tried ^[^\.]*(?!\.json$) but that matches ab in abc.json.
I've come so far as to come up with two regexes that do the job, but I want to have one regex that can do this.
// Match anything that has a dot but does not end in .json
^.*\.(?!json$)
// Match anything that doesn't have a dot
^[^\.]*$
I like http://regex101.com/#javascript to test them.
I am using the regexp as part of ExpressJS route definition in app.get(REGEXP, routes.index).
Try /^(?!.*\.json$).*$/
/^(?!.*\.json$).*$/.test("foo.json")
false
/^(?!.*\.json$).*$/.test("foo")
true
/^(?!.*\.json$).*$/.test("foo.html")
true
You can always just get the file extension and then compare it.
Regex to find file extension
/\.[^.]*$/
get the file extension with
var extension = /\.[^.]*$/.exec("something.json");
if(extension[0] === ".json"){
//do something
}
maybe instead of testing "match (not .json)" you could test "not match (.json)", which is easy ?
I would create a regex to match .json and then when you check for it reverse the logic with a
match == false
That would be much simpler and it shows what you are trying to do making it a lot more obvious to other programmers.