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:
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 2Invalid regular expression: /^(?:?pid=)+/: Nothing to repeat
- 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
6 Answers
Reset to default 3If 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]);