【问题标题】:JavaScript Regex for Path without leading or trailing slashes用于路径的 JavaScript 正则表达式,没有前导或尾随斜杠
【发布时间】:2021-10-01 18:27:56
【问题描述】:

我正在努力找出一种用于 JavaScript 的正则表达式模式,该模式将修剪其前后斜杠(或散列/扩展)的路径

例如:

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

应该返回:

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

我觉得我正在接近以下内容,但它只选择没有任何斜线、哈希或句点的文本。

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

我正在尝试在 vuetify 文本字段验证中将其用于正则表达式,如果这有帮助的话。

结果

我最终得到了这个正则表达式 slug

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

https://regexr.com/66ol9

【问题讨论】:

  • 验证不会改变内容,它只是告诉你它是否匹配正则表达式。
  • 正确。我只想拒绝他们是否有前面或后面的斜杠或井号标签等。
  • 我也试过 (?![\\\/\#\.])[\w\-\\\/_]+(?![\\\/\#\.$ ]+)/gm

标签: javascript regex validation filepath


【解决方案1】:

这是在没有后视的情况下实现的(它们在 Safari 中仍然被拒绝:():

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

regex proof。还有……

解释

--------------------------------------------------------------------------------
  ^                        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))

【讨论】:

  • 感谢您提供此故障。我仍在捕获folder/path#hash,但也想避免这种可能的情况。我很困惑为什么这不会被第一次前瞻捕捉到? regex101.com/r/dYjqWQ/1
  • @broxen 要避免使用哈希符号的字符串,请使用否定字符类 ^(?![#\/.])(?!.*[#\/.]$)[^#]*$。见regex101.com/r/dYjqWQ/2
  • 谢谢,这成功了。
【解决方案2】:

在开头使用否定的lookahead,在结尾使用否定的lookbehind。

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

DEMO

【讨论】:

  • 感谢您帮助我。不幸的是,我没有得到想要的结果。我试过你的演示和regexr.com/66nk9
  • 它似乎在你的正则表达式中工作。所有以/ 开头的路径都被拒绝了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-01
  • 1970-01-01
相关资源
最近更新 更多