Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.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 如何用列表中的第一个URL替换一堆URL_Javascript - Fatal编程技术网

Javascript 如何用列表中的第一个URL替换一堆URL

Javascript 如何用列表中的第一个URL替换一堆URL,javascript,Javascript,首先,如果我的问题有点混乱,我很抱歉,英语不是我的母语,所以我需要用列表中的第一个URL替换一堆URL,URL不断变化,下面是我要替换的文本: [rotate=url1.usa.gov;url2.facebook.com/;http://9gag.com] 唯一不变的是[rotate=..],URL被分割我希望结果为: url1.usa.gov 以下是我尝试过的: var str = str.replace(/\[rotate=.*;.*\]/g, '$0'); 但当我这样做时,结果是:$

首先,如果我的问题有点混乱,我很抱歉,英语不是我的母语,所以我需要用列表中的第一个URL替换一堆URL,URL不断变化,下面是我要替换的文本:

[rotate=url1.usa.gov;url2.facebook.com/;http://9gag.com]
唯一不变的是
[rotate=..]
,URL被
分割我希望结果为:

url1.usa.gov
以下是我尝试过的:

var str = str.replace(/\[rotate=.*;.*\]/g, '$0');
但当我这样做时,结果是:
$0

如何执行此操作?

如果仍要使用正则表达式,可以尝试:

str.replace(/\[rotate=([^;\[\]]*)(;[^\[\];]*)*\].*/g, '$1')
返回

"url1.usa.gov"

请参阅。

如果仍要使用正则表达式,可以尝试:

str.replace(/\[rotate=([^;\[\]]*)(;[^\[\];]*)*\].*/g, '$1')
返回

"url1.usa.gov"

请参阅。

。replace
解释替换字符串中以
$
开头的许多特殊序列,但
$0
不是其中之一

如果要从匹配中提取子字符串,则需要捕获组(
)。与它们匹配的任何内容都可以作为match对象上的属性以及
$1
$2
。。。在替换字符串中

如果是你的话,我会和你一起去

str.replace(/\[rotate=([^;\]]*)(?:;[^\]]*)?\]/g, '$1')
即:

\[rotate=      // find '[rotate=', followed by
(  [^;\]] *  ) // 0 or more characters that are not ';' or ']'
               // (and remember this part as $1),

(?:            // group, but don't capture
   ;           //   a literal ';'
   [^\]] *     //   0 or more characters that are not ']'
)?             // this group is optional
\]             // a literal ']'

即使列表中只有一个URL,
[rotate=example.com]
也会变成
example.com
。replace
解释替换字符串中以
$
开头的许多特殊序列,但
$0
不是其中之一

如果要从匹配中提取子字符串,则需要捕获组(
)。与它们匹配的任何内容都可以作为match对象上的属性以及
$1
$2
。。。在替换字符串中

如果是你的话,我会和你一起去

str.replace(/\[rotate=([^;\]]*)(?:;[^\]]*)?\]/g, '$1')
即:

\[rotate=      // find '[rotate=', followed by
(  [^;\]] *  ) // 0 or more characters that are not ';' or ']'
               // (and remember this part as $1),

(?:            // group, but don't capture
   ;           //   a literal ';'
   [^\]] *     //   0 or more characters that are not ']'
)?             // this group is optional
\]             // a literal ']'

即使列表中只有一个URL,
[rotate=example.com]
也会变成
example.com

使用捕获的组
。替换(/\[rotate=(.*);*\]/,“$1”)
如果不喜欢正则表达式,则首先
拆分(“;”)
,然后再次拆分数组中的第一项
拆分(“=”)
并从中获取第一个元素。@Tushar非常感谢,你解决了我的问题。@Tushar在这里的
[rotate=A]
[rotate=A;B]和一个random]失败。
使用捕获的组
。替换(/\[rotate=(.*);*\]/,'$1')
如果你不喜欢正则表达式,那么首先
拆分(';')
然后在拆分数组的第一项上再次拆分('=')
并从中获取第一个元素。@Tushar非常感谢,你解决了我的问题。@Tushar在
[rotate=A]
[rotate=A;B]和一个random]上失败,在
[rotate=A]和
[rotate=A;B]上失败还有一个随机的]这里
@melpomene我不知道你说的失败是什么意思。你能看到结果吗?如果我理解正确,
[rotate=A;B]
[rotate=A]
都应该让你
A
?@melpomene更新了,但实际上我不喜欢这里的正则表达式,你应该把它分为几个步骤。在这里
[rotate=A]和
[rotate=A;B]失败还有一个随机的]这里
@melpomene我不知道你说的失败是什么意思。你能看到结果吗?如果我理解正确,
[rotate=A;B]
[rotate=A]
都应该更新
A
?@melpomene,但实际上我不喜欢这里的正则表达式,您应该将其分为几个步骤。