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
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);
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);
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]);
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);
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.com/movie13.m3u8?pid=144.21.112.0&tcp=none';
var data = str.split("?");
alert(data[0]);
Related
If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");
I want to replace multiple occurences of comment and try like below
JsFiddle
Code:
var str = '<!--#test--><!--#test1-->'
str = str.replace('<!--/g', '').replace('-->/g', '');
alert(str)
Your problem is that you're trying to use a string instead of a regular expression. For example, this works.
var str = '<!--#test-->'
str = str.replace(/<!--/g, '').replace(/-->/g, '');
alert(str)
Plain regex commands need to be inside //.
Also, use the
Disjunction; Alternative | (pipe character)
str = str.replace(/<!--|-->/g, ''); // #test#test1
I need get from string, piece after last \ or /, that is from this string C:\fake\path\some.jpg result must be some.jpg
I tried this:
var str = "C:\fake\path\some.jpg";
var newstr = str.replace(/[^\\\/]+$/, "");
alert(newstr);
http://jsfiddle.net/J4GdN/3/
but not works, what is right regex for this?
You don't need a regex to do it, this should work:
var newstr = str.substring(Math.max(str.lastIndexOf("\\"), str.lastIndexOf("/")) + 1);
Well your str needs to be escaped \\ for it to work correctly. You also need to use match since the reg exp you are using is matching the end, not the start.
var str = "C:\\fake\\p\ath\some.jpg";
var newstr = str.match(/[^\\/]+$/);
alert(newstr);
JSFiddle
first, let's escape the backslashes
var str = "C:\\fake\\path\\some.jpg";
var newstr = str.replace(/[^\\\/]+$/, "");
alert(newstr);
now, you are removing some.jpg instead of getting it as the result.
here are a few options to fix this:
1.replace with a regex
var newstr = str.replace(/.*[\\\/]/, "");
2.split with regex (doesn't work in all browsers)
var tmp = str.split(/\\|\//);
var newstr = tmp[tmp.length-1];
4.as said here, match
var newstr = str.match(/.*[\\\/](.*)/)[1];
This is my code :
var myStr = "/private_images/last-edit/image-work-med.png";
and I'd like to replace the last 7 chars (med.png) with big.png. Or, as you can see, the last occurence after a - split.
How can I do it? I think about regex, but I'm not a champion with them. Tried :
myStr = myStr .replace(/-([^-]*)$/, "big" + '$1');
but it replace the last -, not the last occurence. So the result is /private_images/last-edit/image-workbigmed.png
I'll make a confession: I'm not so great with regexes either.
How about splitting up using split? Less concise, but easier to understand.
var myStr = "/private_images/last-edit/image-work-med.png";
var strs = myStr.split('-');
// Change the last element.
strs[strs.length - 1] = "big.png";
// And put back the right string.
myStr = strs.join('-');
You could use a regex, or you could use a few string methods and make your intentions clear.
var idx = myStr.lastIndexOf("-");
var newStr = myStr.substring(0, idx) + "big.png";
Without using RegExp you could use:
var str = "/private_images/last-edit/image-work-med.png"
,replace = 'big.png'
,nwstr = str.slice(0,str.lastIndexOf('-')+1)+replace;
//=> nwstr now "/private_images/last-edit/image-work-big.png"
More 'functional':
var nwstr = function(s){
return s.replace(s.substr(-7),'');}(
'/private_images/last-edit/image-work-med.png'
)+'big.png'
var url = "/private_images/last-edit/image-work-med.png";
var index = url.lastIndexOf('-');
url = url.substring(0, index+1);
var url2 = "big.png";
var output = url.concat(url2); alert(output);
Check this
Just add '-' to your regex and to the replacement string:
myStr = myStr .replace(/-([^-]*)\.png$/, "-big.png");
Or if you want the file extension to be variable:
myStr = myStr .replace(/-([^-]*)\.([a-z]+)$/, "-big.$2");
Why not just use replace:
var myStr = "/private_images/last-edit/image-work-med.png";
var newStr = myStr.replace("med.png", "big.png");
According to the requirements specified in your question this would suffice.
If you know it will be a .png file:
var ex = new Regex(#"-\w*.png$");
var myStr = "/private_images/last-edit/image-work-med.png";
myStr = ex.Replace(myStr, "-big.png");
It works but if its a jpg it wont...
If you want to use string functions -
var myStr = "/private_images/last-edit/image-work-med.png";
var cleanedupStr = myStr.slice(0, myStr.lastIndexOf("-"));
String.slice
How do you trim all of the text after a comma using JS?
I have: string = Doyletown, PA
I want: string = Doyletown
var str = 'Doyletown, PA';
var newstr=str.substring(0,str.indexOf(',')) || str;
I added the || str to handle a scenario where the string has no comma
How about a split:
var string = 'Doyletown, PA';
var parts = string.split(',');
if (parts.length > 0) {
var result = parts[0];
alert(result); // alerts Doyletown
}
using regular expression it will be like:
var str = "Doyletown, PA"
var matches = str.match(/^([^,]+)/);
alert(matches[1]);
jsFiddle
btw: I would also prefer .split() method
Or more generally (getting all the words in a comma separated list):
//Gets all the words/sentences in a comma separated list and trims these words/sentences to get rid of outer spaces and other whitespace.
var matches = str.match(/[^,\s]+[^,]*[^,\s]+/g);
Try this:
str = str.replace(/,.*/, '');
Or play with this jsfiddle