Home > Enterprise >  Using regex to match filenames in routes with nodejs
Using regex to match filenames in routes with nodejs

Time:07-15

I am using nodejs and I need check a route for filenames with three or more chars and .dcm or .v2 extension

My files has the next format:

/api/dicom/filename

for sample:

/api/dicom/f:/programas/appprog/dicomserver150c/data/86557/1.3.46.670589.30.1.6.1.116520970982.1481396383125.1_0002_000001_16575404220008.dcm

I am using the next regex and no look:

app.get(/^[A-Za-z_.\/\\:]{3,}.(?:v2|dcm)$/i, (req, res, next) => {
  console.log(req.path)
})

CodePudding user response:

Assuming the root path of your API is /api/dicom, you could try:

app.get(/[a-zA-Z:/0-9._]*.dcm/, (req, res, next) => {
  console.log(req.path)
})

CodePudding user response:

You can try this regex: First part will match 3 or more character. After the first back slash we will match dot (.) and allow an extension between dcm and v2.

(.*[a-z]){3}\.[dcm|v2] $

Full code:

app.get(/(.*[a-z]){3}\.[dcm|v2] $, (req, res, next) => {
  console.log(req.path)
})
  • Related