Escape forward slash in jscript regexp character class?

前端 未结 1 1258
死守一世寂寞
死守一世寂寞 2020-12-07 02:08

I have a regular expression testing for numbers(0-9) and/or forward slashes (/). It looks like this:

/^[0-9/]+$/i.test(value)


        
相关标签:
1条回答
  • 2020-12-07 02:52

    The standard clearly says you can put anything unescaped in a character class except \, ] and newline:

    RegularExpressionClassChar ::
       RegularExpressionNonTerminator but not ] or \
       RegularExpressionBackslashSequence
    
    RegularExpressionNonTerminator ::
        SourceCharacter but not LineTerminator
    

    ( http://es5.github.com/#x7.8.5 ). No need to escape /.

    On the other side, I personally would escape everything when in doubt, just to make less smart parsers happy.

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