最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex remove all string start with special character - Stack Overflow

programmeradmin3浏览0评论

I have a string look like:

var str = .m3u8?pid=144.21.112.0&tcp=none

I want to remove at start ?pid= to end. The result look like:

var str = .m3u8

I tried to:

str = str.replace(/^(?:?pid=)+/g, "");

But it show error like:

Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat

I have a string look like:

var str = https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none

I want to remove at start ?pid= to end. The result look like:

var str = https://sharengay./movie13.m3u8

I tried to:

str = str.replace(/^(?:?pid=)+/g, "");

But it show error like:

Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat

Share Improve this question edited Jun 8, 2018 at 9:13 Kunj 2,0182 gold badges23 silver badges34 bronze badges asked Jun 8, 2018 at 9:11 AveAve 4,4405 gold badges44 silver badges72 bronze badges 2
  • look at this stackoverflow./questions/14988021/… there you should see your problem with jwplayer. If you have a problem with jwplayer edit your question or create a new and don't write it under every answer – Dyragor Commented Jun 8, 2018 at 9:28
  • I was found this before ask question. It's not resolve my problem. – Ave Commented Jun 8, 2018 at 9:31
Add a ment  | 

6 Answers 6

Reset to default 3

If you really want to do this at the string level with regex, it's simply replacing /\?pid=.*$/ with "":

str = str.replace(/\?pid=.*$/, "");

That matches ?pid= and everything that follows it (.*) through the end of the string ($).

Live Example:

var str = "https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);

You can use split

var str = "https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);

You can simply use split(), which i think is simple and easy.

var str = "https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);

You may create a URL object and concatenate the origin and the pathname:

var str = "https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none";

var url = new URL(str);
console.log(url.origin + url.pathname);

You have to escape the ? and if you want to remove everything from that point you also need a .+:

 str = str.replace(/\?pid=.+$/, "")

You can use split function to get only url without query string.

Here is the example.

var str = 'https://sharengay./movie13.m3u8?pid=144.21.112.0&tcp=none';

var data = str.split("?");

alert(data[0]);

发布评论

评论列表(0)

  1. 暂无评论