Regex 正则表达式必须以字母或数字开头,字符串只能有字母、数字或斜杠,不能有双斜杠

Regex 正则表达式必须以字母或数字开头,字符串只能有字母、数字或斜杠,不能有双斜杠,regex,Regex,我正在检查一些字符串。以下是参数 字符串必须以字母或数字开头 字符串只能包含字母、数字或斜杠 字符串不能有双斜杠(例如:“api//go”) 好字符串: go go2/api/hello go/api45 /go (can't begin with a slash) go//api (can't have a double slash) go/api% (can't contain non number, letter or slash) 坏字符串: go go2/

我正在检查一些字符串。以下是参数

  • 字符串必须以字母或数字开头
  • 字符串只能包含字母、数字或斜杠
  • 字符串不能有双斜杠(例如:“api//go”)
  • 好字符串:

    go
    go2/api/hello
    go/api45
    
    /go        (can't begin with a slash)
    go//api    (can't have a double slash)
    go/api%    (can't contain non number, letter or slash)
    
    坏字符串:

    go
    go2/api/hello
    go/api45
    
    /go        (can't begin with a slash)
    go//api    (can't have a double slash)
    go/api%    (can't contain non number, letter or slash)
    
    我一直在尝试使用RegExr.com,但没有用。我一直在尝试这样的表达:

    ^[^\/](([0-9A-Za-z])+(\/)?)+
    
    但是它不太管用。

    给你:

    ^[0-9a-z](\/?[0-9a-z])*\/?$
    
    需要当前格式的
    /i
    (不区分大小写的修饰符)。这是非常简单的,不需要头像

    说明:

    ^         Starts with
    [0-9a-z]  1 Alphanumeric character
    (         Start a repeatable group
    \/?       Optional /
    [0-9a-z]  1 Alphanumeric character
    )*        Repeat the group zero or more times
    \/?       Allow for an ending slash
    $         String must end
    

    您可以尝试类似的方法(我希望您使用的任何口味都具有前瞻性!)


    请查看完整的描述和测试字符串。

    如果您一直在“尝试使用RegExr.com”,当然可以包括一些您尝试过但没有成功的东西?当然,肯,这是我到目前为止得到的^[^\/](0-9A-Za-z])+(\/)+谢谢:-)供将来参考:当被要求在问题中添加信息或详细信息时,请将您的问题添加到评论中,而不要将其放在不易看到且格式不正确的地方。您可以在问题标签下方找到链接。@Jake.JS问得好-HR25太糟糕了,regex101迫使你写
    \/
    ,而不是只写
    /
    ,反正我这样做是出于习惯:)@Lucastzesniewski你可以通过在左边打手势来将分隔符改成其他分隔符,以避免跳出斜杠:@Robin nice catch!我不知道。谢谢。@Lucastzesniewski接球不错,我去掉了+来消除它