Matching exact string with JavaScript

后端 未结 5 824
离开以前
离开以前 2020-11-28 10:49

How can I test if a RegEx matches a string exactly?

var r = /a/;
r.test(\"a\"); // returns true
r.test(\"ba\"); // returns true
testExact(r, \"ba\")         


        
5条回答
  •  一生所求
    2020-11-28 11:25

    If you do not use any placeholders (as the "exactly" seems to imply), how about string comparison instead?

    If you do use placeholders, ^ and $ match the beginning and the end of a string, respectively.

提交回复
热议问题