开发者

about complicated RegExp [closed]

开发者 https://www.devze.com 2023-03-30 12:13 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post.
Closed. This question needs details or clarity. It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 9 years ago.

Improve this question 开发者_如何学Go

I need a regexp to check the String "incould \w,-,. and not start with ."

I hope the result is

  • abc ->false
  • ab_c ->false

  • a-b_c.->false

  • a@-b_C. ->true

  • .a-b_c. ->true

  • I tried /[^\w-\.]/ only ".a-b_c." was fail, I get false (I hope be true) and I tried /^\./ can be ".a-b_c." true, but other was fail.

    has any body can help me?


    If you need to match string including only \w, - or . and not starting with ., then try this:

    /^(?!\.)[\w.-]+$/
    

    Details:

    • ^ - search from start of string
    • (?!\.) - don't match if there is . symbol at this position
    • [\w.-]+ - match to more than 1 symbols from \w.- set
    • $ - match to end of string

    Testing:

    • abc -> matched
    • ab_c -> matched
    • a-b_c. -> matched
    • a@-b_C. -> not matched
    • .a-b_c. -> not matched
    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消