I need to remove size i.e. resolution of image path from url. Take following example:
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg
I need to remove -1024x682
http://raudevlocal.com/wp/wp-content/uploads/2017/05/rohit-300x118.jpg
Need to remove -300x118
use regex pattern
$(document).ready(function(){
window.location.href = window.location.href.replace(/(-\d{2-6}x\d{2-6})/g,'');
});
str = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
str.replace(/-(\d+x\d+)/, "");
output will be
http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake.jpeg
use regular expression
var string = "http://raudevlocal.com/wp/wp-content/uploads/2017/05/0000362_chocolate-layer-cake-1024x682.jpeg";
console.log(string.replace(/-\d+x\d+/g,''));
window.loaction.href= window.location.href.replace(/-\d+x\d+/g,'');
You can use replace function
window.location.href.replace('-1024x682', '');
If you want to use the new one as your current url then try
window.location.href = window.location.href.replace('-1024x682', '');
Related
I have a folder path that always starts with a certain string which I want to remove. Let's say it looks like this:
my-bucket/2929023/32822323/file.jpg
I want it to look like this:
2929023/32822323/file.jpg
How would I do that? Thanks!
Using the functions substring and indexOf from String.prototype.
var str = "my-bucket/2929023/32822323/file.jpg";
console.log(str.substring(str.indexOf('/') + 1))
You could use a simple replace method if the string is only present once;
var string = "my-bucket/2929023/32822323/file.jpg";
var revisedString = string.replace('my-bucket/', '');
console.log(revisedString);
However, you're also able to use a Regex (regular expression) to remove it as well, something like;
var string = "my-bucket/2929023/32822323/file.jpg";
console.log(string.replace(/^my-bucket\//, ''));
Use a regex to rip the first one out. No substrings necessary.
var myString= "my-bucket/2929023/32822323/file.jpg";
myString = myString.replace(/^.+?[/]/, '');
I have one URL which is
https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1
I want this 'service_enu_dv2_oui' part using regex. How can I get that?
Please suggest.
Thanks.
this is without regex but working nicely: http://jsfiddle.net/mKNkF/
var path = "https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1";
var pathArray = path.split( '/' );
alert(pathArray[3]);
If the path only has a single segment as shown in your example, the regex is fairly straight-forward:
/^http(?:s?):\/\/[^\/]+\/([^\/]+)/
Live link with details.
For example:
var str = "https://hpscrmdev.honeywell.com/service_enu_dv2_oui/start.swe?SWECmd=GotoView&SWEView=Home+Page+View+(WCC)&SWERF=1&SWEHo=hpscrmdev.honeywell.com&SWEBU=1";
var match = str.match(/^http(?:s?):\/\/[^\/]+\/([^\/]+)/);
if (match) {
console.log(match[1]); // "service_enu_dv2_oui"
}
Path without host and query you can receive from
window.location.pathname
http://www.w3schools.com/js/js_window_location.asp
No regex required
https?:\/\/[\w\.]+\/(\w+)\/
this regex have one match group
you check this link
why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it must become http://myweb.com/page/2/id/2
You need to do two things:
change str.replace to url.replace
remove the ' around the regex
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url rather than str, if you want to replace something in the String stored in the url variable.
You have a naming misspell.
Rename your url var to str or change the str.replace to url.replace:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');
I have a string, for example testserver\sho007, how do I use jQuery to return only sho007?
You can do it simply with javascript
var my_string="testserver\sho007";
var left_side=my_string.split("\\")[0];
var right_side=my_string.split("\\")[1];
edited to add the double slash as Eric mentioned
You may use
text.substring(fromindex,toindex)
Use Regular Expressions if you are sure that the text you want is going to be after the slash.
split the string using "\" and get the position [1], always start with [0]
exp:
html:
<a class='test'>testserver\sho007</a>
js:
$(document).ready(function(){
var a = $(".test").text();
var aResult = a.split("\\")[1];
});
testserver = [0]
sho007 = [1]
Let's say I have something like this:
var location = '/users/45/messages/current/20/';
and I need to end up with this:
'/45/messages/current/20/'
So, I need to erase the first part of /whatever/
I can use jquery and/or javascript. How would I do it in the best way possible?
To replace everything up to the second slash:
var i = location.indexOf("/", 1); //Start at 2nd character to skip first slash
var result = location.substr(i);
You can use regular expressions, or the possibly more readable
var location = '/users/45/messages/current/20/';
var delim = "/"
alert(delim+location.split(delim).slice(2).join(delim))
Use JavaScript's slice() command. Do you need to parse for where to slice from or is it a known prefix? Depending on what exactly you are parsing for, it may be as simple as use match() to find your pattern.
location.replace("/(whatever|you|want|users|something)/", "");
find the 2nd index of '/' in the string, then substr from the index to the end.
If you always want to eliminate the first part of a relative path, it's probably simplest just to use regular expressions
var loc = '/users/45/messages/current/20/';
loc.replace(/^\/[^\/]+/,'');
location.replace(/\/.*?\//, "/");
location.replace(/^\/[^\/]+/, '')