Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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 为什么这不是一个有效的正则表达式?_Javascript_Regex - Fatal编程技术网

Javascript 为什么这不是一个有效的正则表达式?

Javascript 为什么这不是一个有效的正则表达式?,javascript,regex,Javascript,Regex,我试图使用regex.exec()it从URL检索字符串的一部分,但由于某种原因,我收到了一个错误。希望你们能发现 我正试图从字符串->http://anotherdomain.com/image.jpg var haystack = 'http://domain.com/?src=http://anotherdomain.com/image.jpg&h=300'; var needle = /(?<=src=).+(?=&h)/; var results = needle

我试图使用
regex.exec()
it从URL检索字符串的一部分,但由于某种原因,我收到了一个错误。希望你们能发现

我正试图从字符串->
http://anotherdomain.com/image.jpg

var haystack = 'http://domain.com/?src=http://anotherdomain.com/image.jpg&h=300';
var needle = /(?<=src=).+(?=&h)/;
var results = needle.exec(haystack);
var haystack='1〕http://domain.com/?src=http://anotherdomain.com/image.jpg&h=300';
变量指针=/(?
SyntaxError:无效量词


因此,我尝试在指针周围添加单引号,但不起作用。添加引号会给我提供
指针。exec
不是一个函数。

Javascript正则表达式不支持查找

您或许可以通过以下方式度过难关:

var haystack = 'http://domain.com/?src=http://anotherdomain.com/image.jpg&h=300';
var needle = /src=(.+)(?=&h)/;
var results = needle.exec(haystack);

// results is now ["src=http://anotherdomain.com/image.jpg", "http://anotherdomain.com/image.jpg"], so haystack[1] is what you want.

Javascript正则表达式不支持查找

您或许可以通过以下方式度过难关:

var haystack = 'http://domain.com/?src=http://anotherdomain.com/image.jpg&h=300';
var needle = /src=(.+)(?=&h)/;
var results = needle.exec(haystack);

// results is now ["src=http://anotherdomain.com/image.jpg", "http://anotherdomain.com/image.jpg"], so haystack[1] is what you want.

为什么不使用捕获参数而不是查找:

results = haystack.match(/\?src=([^&]*)&/);

if (results) {
    result = results[1];
}

为什么不使用捕获参数而不是查找:

results = haystack.match(/\?src=([^&]*)&/);

if (results) {
    result = results[1];
}

你知道什么是JS中的lookbehind的等价物吗?谢谢!这很有效…我不知道我不能在JS中使用lookbehind..它似乎在PHP中工作…@Rick:是的,PHP支持lookbehinds。你知道什么是JS中的lookbehind的等价物吗?谢谢!这很有效…我不知道我不能在JS中使用lookbehind..它似乎在PHP中工作…@Rick:是的,PHP支持lookbehinds.var URL=haystack.split(“src=”)[1]。split(&”)[0];var URL=haystack.split(“src=”)[1]。split(&”)[0];