.match() with a regular expression returns null

前端 未结 3 2072
遥遥无期
遥遥无期 2020-12-17 08:29

I am trying to do something I thought would be pretty easy to do, which is to restrict a string to certain characters by matching a regular expression.

var v         


        
3条回答
  •  执念已碎
    2020-12-17 08:56

    Regex must be surrounded with /, not ', so that JavaScript creates a variable of type regex, not of type string. So for instance, for your ALPHA case, you should have

    regex = /^[a-zA-Z]+$/;
    

    See MDN's page on .match for more information about using .match.

提交回复
热议问题