[英]Find Replace Short Part of URL using Javascript
Assume I have the following URL stored in variable called content
: 假设我将以下URL存储在名为
content
变量中:
http://www.example.com/watch?v=4444444&feature=related
Problem: 问题:
The final output would look like: 最终输出如下所示:
http://www.example.com/embed/4444444
I tried these two steps but didn't work: 我尝试了以下两个步骤,但没有成功:
content = content.replace('/watch?v=/', 'embed/');
content = content.replace('&*/g','');
The URL in page source code appears as: 页面源代码中的URL显示为:
http://www.example.com/watch?v=4444444&feature=related
You have many errors: 您有很多错误:
'.*'
, not just '*'
. '.*'
,而不仅仅是'*'
。 The star modifies the previous token. g
flag here. g
标志。 Try this instead: 试试这个:
content = content.replace('watch?v=', 'embed/').replace(/&.*/, '');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.