2

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

3
  • Validation doesn't change the contents, it just tells you if it matches the regexp or not. Commented Oct 1, 2021 at 18:35
  • Correct. I just want to reject if they have a preceding or trailing slash, or hash tag, etc. Commented Oct 1, 2021 at 18:40
  • I also tried (?![\\\/\#\.])[\w\-\\\/_]+(?![\\\/\#\.$]+)/gm Commented Oct 1, 2021 at 18:43

2 Answers 2

3

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))
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this breakdown. I'm still capturing folder/path#hash but would like to avoid this possible case as well. I'm confused why this wouldn't be captured by the first lookahead? regex101.com/r/dYjqWQ/1
@broxen To avoid strings with hash symbol, use negated character class, ^(?![#\/.])(?!.*[#\/.]$)[^#]*$. See regex101.com/r/dYjqWQ/2
Thank you, this did the trick.
1

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

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

DEMO

2 Comments

Thank you for helping me. Unfortunately, I'm not getting the desired results. I tried your demo and regexr.com/66nk9
It seems to be working at your regexr. All the paths beginning with / were rejected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.