Regular Expression To Match String Not Starting With Or Ending With Spaces

前端 未结 2 1625
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 00:37

I need a regular expression that makes sure a string does not start with or end with a space. I don\'t care if it has a space in the \"middle\" just not at the beginning or the

2条回答
  •  日久生厌
    2021-01-23 01:17

    You use lookaround assertions, because they're zero-width:

    ^(?=\S).*(?<=\S)$
    

    It might be better to use negative assertions and positive character classes, though:

    ^(?!\s).*(?<=\s)$
    

提交回复
热议问题