Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Sinon fakeserver url regexp_Javascript_Regex_Unit Testing_Sinon - Fatal编程技术网

Javascript Sinon fakeserver url regexp

Javascript Sinon fakeserver url regexp,javascript,regex,unit-testing,sinon,Javascript,Regex,Unit Testing,Sinon,根据,Sinon fakeserver可以使用regexp模式匹配URL: respondWith(方法,urlRegExp,response) 我想匹配所有以foo=1结尾的URL。以下是我的尝试: this.server.respondWith('GET', '/foo=1$/', [200, { 'Content-Type': 'application/json' }, '{ "foo": 1 }']); 然而,它似乎不起作用。我的regexp可能是错误的,但是我需要你的帮助来调整它

根据,Sinon fakeserver可以使用regexp模式匹配URL:

respondWith(方法,urlRegExp,response)

我想匹配所有以
foo=1
结尾的URL。以下是我的尝试:

this.server.respondWith('GET', '/foo=1$/', [200, 
{ 'Content-Type': 'application/json' }, '{ "foo": 1 }']);
然而,它似乎不起作用。我的regexp可能是错误的,但是我需要你的帮助来调整它


正则表达式周围有单引号,因此sinon将其视为字符串(即:)

去掉单引号,它将成为一个正则表达式,并按照您的预期工作

this.server.respondWith('GET',/foo=1$/,[200,
{'Content-Type':'application/json'},{'foo:1}']


请参阅MDN上的参考。

查看该站点上的示例:
server.respondWith(/\/todo items\/(\d+),function(xhr,id){xhr.respond(200,{Content Type:?application/json},[{id:+id+}];})。有一个使用
id
函数
,而
id
是正则表达式中的第一个捕获组。也许,您还需要使用
/foo=(\d+)/
regex捕获组,并将
id
与函数一起使用。