Partial matching a string against a regex

前端 未结 6 1764
梦毁少年i
梦毁少年i 2021-02-04 01:02

Suppose that I have this regular expression: /abcd/ Suppose that I wanna check the user input against that regex and disallow entering invalid characters in the input. When user

6条回答
  •  死守一世寂寞
    2021-02-04 01:59

    This is the hard solution for those who think there's no solution at all: implement the python version (https://bitbucket.org/mrabarnett/mrab-regex/src/4600a157989dc1671e4415ebe57aac53cfda2d8a/regex_3/regex/_regex.c?at=default&fileviewer=file-view-default) in js. So it is possible. If someone has simpler answer he'll win the bounty.

    Example using python module (regular expression with back reference):

    $ pip install regex
    $ python
    >>> import regex
    >>> regex.Regex(r'^(\w+)\s+\1$').fullmatch('abcd ab',partial=True)
    
    

提交回复
热议问题