2

I have a string look like:

var str = https://sharengay.com/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.com/movie13.m3u8

I tried to:

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

But it show error like:

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

Kunj
  • 1,980
  • 2
  • 22
  • 34
Ave
  • 4,338
  • 4
  • 40
  • 67
  • look at this https://stackoverflow.com/questions/14988021/how-do-i-embed-this-m3u8-into-jw-player 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 Jun 08 '18 at 09:28
  • I was found this before ask question. It's not resolve my problem. – Ave Jun 08 '18 at 09:31

6 Answers6

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.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.replace(/\?pid=.*$/, "");
console.log(str);
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

You can use split

var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none"
var result = str.split("?pid=")[0];
console.log(result);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
2

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

var str = "https://sharengay.com/movie13.m3u8?pid=144.21.112.0&tcp=none";
str = str.split("?pid");
console.log(str[0]);
Akhil Aravind
  • 5,741
  • 16
  • 35
1

You may create a URL object and concatenate the origin and the 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);
31piy
  • 23,323
  • 6
  • 47
  • 67
  • I using jwplayer to play m3u8 file. I tried to add url like: "ne.hdonline.vn/d6a1dd9bcae0baa82aa51eab9ac95d43/s6/012018/01/…; into player, it not working, can you suggest any solution? – Ave Jun 08 '18 at 09:16
  • @vanloc -- Does the script run in IE? – 31piy Jun 08 '18 at 09:17
  • No. It only for Chrome and Firefox latest version. I don't need it working in IE. My code like: options.file = obj["file"].replace(/\?pi=.*$/, ""); jwplayer('my_video').setup(options); – Ave Jun 08 '18 at 09:18
1

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

 str = str.replace(/\?pid=.+$/, "")
Ahmed Bajra
  • 391
  • 1
  • 2
  • 14
1

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

Here is the example.

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

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

alert(data[0]);

tj cloud
  • 21
  • 7