How to restrict some file types and allow all others in Regular expression Asp.net

前端 未结 1 1752
我寻月下人不归
我寻月下人不归 2021-01-28 07:47

I want to restrict few file types format and allow all others in regular expression validation expression. What i have try is specify some allowed and some restricted file types

相关标签:
1条回答
  • 2021-01-28 08:03

    Use a negative lookbehind:

    /^.*(?<!\.(exe|bat|msi))$/i
    

    The negative lookaheads you're using aren't helping you. At all. They're trivially true because you're trying to match them at the end of the string, and lookarounds don't consume anything, so the last position in the string can't have exe or bat after it.

    A step by step explanation, for posterity's sake:

    ^
    

    Match the start of the string, as I'm sure you know.

    .*
    

    consume the whole string.

    (?<! ... )
    

    Look back and make sure we haven't consumed....

    \.
    

    A literal dot, followed by...

    (exe|bat|msi)
    

    any of our verbotten file types.

    $
    

    then match the end of the string.

    I also chose to make it case insensitive.

    Edit, for js:

    /^(?:(?!\.(exe|bat|msi)$).)*/i
    

    Moar different explanation:

    ^
    

    Top of string

    (
    

    Start group

    .
    

    Arbitrary Character

    (?!...)
    

    Negative lookahead. Not followed by:

    \.
    

    Literal dot.

    (exe|bat|msi)
    

    Forbidden File types.

    $
    

    End of string

    )*

    Close group and match that an arbitrary number of times.

    0 讨论(0)
提交回复
热议问题