Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Conditional - Fatal编程技术网

Javascript 如何使用'&';还是'';作为第一个字符

Javascript 如何使用'&';还是'';作为第一个字符,javascript,regex,conditional,Javascript,Regex,Conditional,我正在尝试使用以下字符串进行正则表达式调用: if ((/&timeout=true|?scn=header/i).test(url)) 。。。声明继续。我在控制台中收到此错误: 未捕获的语法错误:无效的正则表达式:/&timeout=true |?scn=header/:无需重复 我该如何恰当地表达这一观点 试试/(&timeout=true | \?scn=header)/i ??是正则表达式中的特殊字符,因此需要对其进行转义尝试/(&timeout=true | \?scn=he

我正在尝试使用以下字符串进行正则表达式调用:

if ((/&timeout=true|?scn=header/i).test(url))
。。。声明继续。我在控制台中收到此错误:

未捕获的语法错误:无效的正则表达式:
/&timeout=true |?scn=header/
:无需重复

我该如何恰当地表达这一观点

试试
/(&timeout=true | \?scn=header)/i

??是正则表达式中的特殊字符,因此需要对其进行转义

尝试
/(&timeout=true | \?scn=header)/i


??是正则表达式中的特殊字符,因此需要将其转义

必须将问号字符转义,因为它是正则表达式运算符:

if ((/&timeout=true|\?scn=header/i).test(url))

您必须转义问号字符,因为它是正则表达式运算符:

if ((/&timeout=true|\?scn=header/i).test(url))

问题是,
有一个特殊的含义,即重复零次或一次,因此会出现错误。逃避它吧:

/&timeout=true|\?scn=header/

问题是,
有一个特殊的含义,即重复零次或一次,因此会出现错误。逃避它吧:

/&timeout=true|\?scn=header/
这似乎有效

/\&timeout=true | \/?scn=header/i

这似乎有效

/\&timeout=true | \/?scn=header/i

演示:

试试这个:

/[&\?]timeout=true |[&\?]scn=header/

两者都匹配

http://example.com/file.html?scn=header&timeout=true
http://example.com/file.html?timeout=true&scn=header
因为我们不知道哪个参数先出现

演示:

试试这个:

/[&\?]timeout=true |[&\?]scn=header/

两者都匹配

http://example.com/file.html?scn=header&timeout=true
http://example.com/file.html?timeout=true&scn=header

因为我们不知道哪个参数先出现

,所以可以选择正斜杠。Aka字符串
/scn=header
scn=header
将匹配。它不会尝试匹配您最初想要的
?scn=header
。@ndn是的,您是对的,但它适用于我目前需要的内容这将使正斜杠成为可选。Aka字符串
/scn=header
scn=header
将匹配。它不会试图匹配您最初想要的
?scn=header
。@ndn是的,您是对的,但它可以满足我目前的需要