Matching exact string with JavaScript

后端 未结 5 831
离开以前
离开以前 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:34

    var data =   {"values": [
        {"name":0,"value":0.12791263050161572},
        {"name":1,"value":0.13158780927382124}
    ]};
    
    //JSON to string conversion
    var a = JSON.stringify(data);
    // replace all name with "x"- global matching
    var t = a.replace(/name/g,"x"); 
    // replace exactly the value rather than all values
    var d = t.replace(/"value"/g, '"y"');
    // String to JSON conversion
    var data = JSON.parse(d);
    

提交回复
热议问题