Regex that matches anything not ending in .json - javascript

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.

Related

Regex expression for exactly known pattern without "cutting into" the string not working

I am currently developing a web-application where I work with java, javascript, html, jquery, etc. and at some point I need to check that whether an input matches a known pattern and only proceed if it is true.
The pattern should be [at least one but max 3 numbers between 0-9]/[exactly 4 numbers between 0-9], so the only acceptable variations should be like
1/2014 or 23/2015 or 123/2016.
and nothing else, and I CANNOT accept something like 1234/3012 or anything else, and this is my problem right here, it accepts everything in which it can find the above pattern, so like from 12345/6789 it accepts and saves 345/6789.
I am a total newbie with regex, so I checked out http://regexr.com and this is the code I have in my javascript:
$.validator.addMethod("hatarozat", function(value, element) {
return (this.optional(element) || /[0-9]{1,3}(?:\/)[0-9]{4}/i.test(value));
}, "Hibás határozat szám!");
So this is my regex: /[0-9]{1,3}(?:\/)[0-9]{4}/i
which I built up using the above website. What could be the problem, or how can I achived what I described? I tried /^[0-9]{1,3}(?:\/)[0-9]{4}$/ibut this doesn't seem to work, please anyone help me, I have everything else done and am getting pretty stressed over something looking so simple yet I cannot solve it. Thank you!
Your last regex with the anchors (^ and $) is a correct regex. What prevents your code from working is this.optional(element) ||. Since this is a static thing, and is probably true, so it does not show any error (as || is an OR condition, if the first is true, the whole returns true, the regex is not checked at all).
So, use
return /^[0-9]{1,3}\/[0-9]{4}$/.test(value);
Note you do not need the (?:...) with \/ as the grouping does not do anything important here and is just redundant. The anchors are important, since you want the whole string to match the pattern (and ^ anchors the regex at the start of the string and $ does that at the end of the string.)
You need use the the following special characters in your regex expression:
^ and $
or \b
so 2 regexp will be correct:
/\b[0-9]{1,3}(?:\/)[0-9]{4}\b/i;
or
/^[0-9]{1,3}(?:\/)[0-9]{4}$/i

Find all `*.html` but not `*.tmp.html` using JavaScript regex

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

javascript regex exclude dot

Hello I'm finding difficulties making the right regex. I'm missing something, but I don't know what.
pattern:
href=".*?\/FileBrowser\/File\?path=esoft\/[^.\s]*?"
test string:
dfhgndfhkljh;fth href="/FileBrowser/File?path=esoft/test/I4/I0000/as.jpeg" dfghfdhnjfgh e:small;"><a href="/FileBrowser/File?path=esoft/test/bb/2evo/1_folder" target="_blank"dsadsadsa
and the site I use to test online is https://regex101.com/r/mU5vH6/2
The goal is to mark the links (after the href) separately as shown https://regex101.com/r/mU5vH6/3 here, but if one of them has a dot - meaning file path, not to be included
You can use this regex:
href="[^"]*\/FileBrowser\/File\?path=esoft([^.])*?"
The previous one was matching:
dfhgndfhkljh;fth href="/FileBrowser/File?path=esoft/test/I4/I0000/as.jpeg" dfghfdhnjfgh e:small;"><a href="/FileBrowser/File?path=esoft/test/bb/2evo/1_folder" target="_blank"dsadsadsa
|___________________________________________________________________________________________________________________________________________|
Because you allowed your match to contain ", which consumed too much chars

javascript regexp to match path depth

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.

Regex extraction of one letter inside html chunk

Hi need to extract ONE letter from a string.
The string i have is a big block of html, but the part where i need to search in is this text:
Vahvistustunnus M :
And I need to get the M inside the nbsp's
So, who is the quickest regex-guru out there? :)
Ok, according to this page in the molybdenum api docs, the results will be all of the groups concatenated together. Given that you just want the char between the two 's then it's not good enough to match the whole thing and then pull out the group. Instead you'll need to do something like this:
(?<=Vahvistustunnus )[a-zA-Z](?= )
Warning
This might not work for you because lookbehinds (?<=pattern) are not available in all regex flavors. Specifically, i think that because molybdenum is a firefox extension, then it's likely using ECMA (javascript) regex flavor. And ECMA doesn't support lookbehinds.
If that's the case, then i'm gonna have to ask someone else to answer your question as my regex ninja (amateur) skills don't go much further than that. If you were using the regex in javascript code, then there are ways around this limitation, but based on your description, it sounds like you have to solve this problem with nothing but a raw regex?
Looks like it uses JavaScript and if so
var str = "Vahvistustunnus M :";
var patt = "Vahvistustunnus ([A-Z]) :";
var result = str.match(patt)[1];
should work.

Categories