[英]Regex remove all string start with special character
我有一个字符串看起来像:
var str = https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none
我想从开始删除?pid=
结束。 结果如下:
var str = https://sharengay.com/movie13.m3u8
我试过了:
str = str.replace(/^(?:?pid=)+/g, "");
但是它显示如下错误:
无效的正则表达式:/ ^(?:?pid =)+ /:无需重复
您可以创建一个URL对象,并将origin
和pathname
:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"; var url = new URL(str); console.log(url.origin + url.pathname);
您可以使用拆分
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none" var result = str.split("?pid=")[0]; console.log(result);
如果您真的想使用正则表达式在字符串级别执行此操作,只需将/\\?pid=.*$/
替换为""
:
str = str.replace(/\?pid=.*$/, "");
匹配?pid=
及其?pid=
所有内容( .*
)直至字符串( $
)的末尾。
现场示例:
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"; str = str.replace(/\\?pid=.*$/, ""); console.log(str);
您必须逃脱?
如果要从那时删除所有内容,则还需要一个.+
:
str = str.replace(/\?pid=.+$/, "")
您可以使用拆分功能仅获取不含查询字符串的网址。
这是例子。
var str =' https ://sharengay.com/movie13.m3u8?pid= 144.21.112.0& tcp= none ';
var data = str.split(“?”);
警报(数据[0]);
您可以简单地使用split(),我认为这很简单。
var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"; str = str.split("?pid"); console.log(str[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.