I’ve started studying RegexOne, and there’s a exercise in which we must capture a piece of a string until the "." , as long as the string ends with .pdf
match file_a_record_file.pdf
match file_yesterday.pdf
skip testfile_fake.pdf.tmp
But I wanted to get a bit deeper and capture this piece of a string, unless the string contains more than 3 characters after a dot character "."
I tried using
^(\w+(?!([.](.{4,}))$))
but of course it didn’t work. How could I correct this pattern, considering the JavaScript RegEx library? (I don’t want a function, only a pattern, if that’s possible).
I guess it would be more flexible if I could avoid using $, but I’d accept any answer matching the question. Thank you all in advance.
>Solution :
I belive this is what you are trying to do:
mySting.match(/(.*)\.pdf$/)
Match file_a_record_file.pdf
Match file_yesterday.pdf
null testfile_fake.pdf.tmp
Edit: the string without the extension is stored on mySting.match(/(.*)\.pdf$/)[1]
For the 3 characters match after the first dot
myString.match(/^([^\.]+)\.[\w]{1,3}$/);
Explanation: match everything that is not a dot + match the first dot + match a word that contains 1 to 3 characters at the end of string