0
/^(file.+)\.pdf$/g

This should be capturing the file names except the extension. But that is not happen in my case.

enter image description here

Got this solution from this site https://regexone.com/lesson/capturing_groups

Ankur Marwaha
  • 1,613
  • 2
  • 15
  • 30
  • 1
    there is a full match and group. have a look: https://regex101.com/r/HgqDji/1 – Navyseal Sep 27 '17 at 17:53
  • 2
    Remove `^` and `$` as what you're looking for is not always at the start and at the end of string (at the same time or otherwise). – Walk Sep 27 '17 at 17:54
  • You're getting *no* matches because your regex requires input that begins with `file` (but your input begins with `Capture`). If you add a multiline `m` flag (see the "Flags" menu on regexr.com) then the regex will run against each line individually instead of the whole input at once. – apsillers Sep 27 '17 at 17:57
  • As for why it still captures the extension when you do that, see the duplicate, or use a lookahead: `/^file.+(?=.pdf$)/` – apsillers Sep 27 '17 at 17:59
  • @Walk solved my issue. You can write this as an answer. – Ankur Marwaha Sep 27 '17 at 17:59
  • I can't as this post is already marked as duplicate, glad I could help :) – Walk Sep 27 '17 at 18:02
  • "file_record_transcript.pdf file_07241999.pdf testfile_fake.pdf.tmp ".match(/(file.+)\.pdf/g) ------- this should get me array of filenames in the console. But that's not happening :( – Ankur Marwaha Sep 27 '17 at 18:05
  • Check duplicate for more info about matching groups. However your regular expression in this case should be "\b(file[^\s]+)\.pdf\b" (notice boundary tags and NOT whitespace inside filename). Check this codepen https://codepen.io/anon/pen/yzbyBw – Walk Sep 27 '17 at 19:49

0 Answers0