Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
JavaScript Regex for Path without leading or trailing slashes

I'm struggling to figure out a Regex pattern for JavaScript that will trim a path of it's preceding and trailing slashes (or hashes/extensions)

For example:

path/
/path
/folder/path/
/folder/folder/path
/folder/path#hash
/folder/path.ext

Should return:

path
path
folder/path
folder/folder/path
folder/path
folder/path

I felt like I was getting close with the following, but it only selects text without any slashes, hashes, or periods.

^([^\\\/\#\.]*)(?![\#\.\\\/].*$)/gm

I'm trying to use this for Regex in a vuetify text-field validation, if that's at all helpful.

Result

I ended up with this regex slug

/^(?![\#\/\.\$\^\=\*\;\:\&\?\(\)\[\]\{\}\"\'\>\<\,\@\!\%\`\~\s])(?!.*[\#\/\.\$\^\=\*\;\:\&\?\(\)\[\]\{\}\"\'\>\<\,\@\!\%\`\~\s]$)[^\#\.\$\^\=\*\;\:\&\?\(\)\[\]\{\}\"\'\>\<\,\@\!\%\`\~\s]*$/

https://regexr.com/66ol9

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is how it is achieved with no lookbehinds (they are still rejected in Safari :():

^(?![#\/.])(?!.*[#\/.]$).*

See regex proof. And...

EXPLANATION

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [#\/.]                   any character of: '#', '\/', '.'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    [#\/.]                   any character of: '#', '\/', '.'
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                             the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
about 4 years ago · Juan Pablo Isaza Report

0

Use a negative lookahead at the beginning, and negative lookbehind at the end.

/^(?![#\/.]).*(?<![#\/.])$/

DEMO

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!