I have a variable that returns me the url of an image, and in this url the id of the image can vary the quantity of numbers... what would be the best way to complement the id like this:
As it is: http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg
How to stay: http://mydomain.com.br/arquivos/ids/1318090-500-500/94842_1.jpg
In short, I need to add the "-500-500" into id
Sorry... A Example:
var a = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var b = "-500-500";
var position = 43;
var output = [a.slice(0, position), b, a.slice(position)].join('');
console.log(output);
You can do something like following:
var url = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var index = url.lastIndexOf("/");
var new_url = url.slice(0, index) + "-500-500" + url.slice(index);
link = link.split("/").reduce((r,c,i)=>r+c+i===5?"-500-500": "","").join("/");
Simply add it behind the 6th element ( that are seperated by / ) .
you have not shown any progress what you did! How about i gave you the hint?
assign a variable to your string, split it using \ and then find the exact occurrence to change by this [0] of course change the number according to your desire where change is required.
Now get your new string and concate with old in a way that you get desired results. Example
var str = "http://mydomain.com.br/arquivos/ids/1318090/94842_1.jpg";
var newstr = str.split('/')[5];
var prepstr = newstr + "something";
then finally,
str + prepstr;
Related
I have this URL:
http://test.com/en/country/city
Im using lastIndexOf to obtain "city" but I want to obtain "country"
window.location.href.substring(window.location.href.lastIndexOf('/') + 1);
Is this posible with lasIndexOf or there is another function available for this case?
Thank you in advance!
You can split your url by / and take needed element (first remove http:// part):
var str = 'http://test.com/en/country/city';
str = str.replace('http://', '');
var parts = str.split('/');
console.log(parts);
alert(parts[2]+', '+parts[3]);
Try:
var fragment = "http://test.com/en/country/city";
var array_fragment = fragment.split('/');
var city = array_fragment[array_fragment.length - 1]
var country = array_fragment[array_fragment.length - 2]
alert(country)
You could do something like this:
var url = "http://test.com/en/country/city"
var urlParts = url.split("/")
urlParts[urlParts.length - 1] (which would equal to "city")
urlParts[urlParts.length - 2] (which would equal to "country")
Basically split on each occurence of "/" and pick the correct item from the returned array.
Is this posible with lastIndexOf?
Yes, it is possible.
Let's say
x="http://test.com/en/country/city"
We get the position of the last /
y=x.lastIndexOf("/");//26
We get the position of the second last /
z=x.lastIndexOf("/",y-1);//18
To extract the country, we now use substring as follows
x.substring(z+1,y)//country
Use split function to get second last value.
Try this
var url='http://test.com/en/country/city';
var splitArray=url.split('/');
alert(splitArray[splitArray.length-2]);
Try something like this.
var url = window.location.href;
var urlArray = url.split("/");
urlArray[urlArray.length-1] //City
urlArray[urlArray.length-2] //Country
Not sure how I would go about writing a formula to do this... I need to do:
current_url = "http://something.random.com/something/something/upload/something/something_else"
new_url = "http://something.random.com/something/something/upload/{NEWSOMETHING}/something/something_else"
Basically I'm always trying to insert another string segment exactly after the upload/ in the original URL. I've thought about using position but I don't have a fixed position because current_url won't always be the same length. The only constant is that I know the string needs to be inserted after upload/, wherever it may be
current_url.replace("upload/","upload/{NEWSOMETHING}/")
If your string is var current_url = "http://something.random.com/something/something/upload/something/c_fit/something_else/c_fit/"; and you want to replace everything in between upload and the last c_fit then current_url.replace(/\/upload\/.*\/c_fit\//,"/upload/"+"<YOUR_STRING>"+"/c_fit/") but you just want to replace between upload and the first c_fit then current_url.replace(/\/upload\/.*?\/c_fit\//,"/upload/"+"<YOUR_STRING>"+"/c_fit/")
var current_url = "http://something.random.com/something/something/upload/something/something_else";
var chunks = current_url.split("/");
var str = [];
var s = chunks.shift();
while(s != "upload"){
str.push(s);
s = chunks.shift();
}
var new_url = str.join('/')+"/upload/{new something}/something/something_else";
alert(new_url);
You could easily split the string on the static text "upload".
var current_url = "http://something.random.com/something/something/upload/something/something_else";
var splitArray = current_url.split("upload");
var additionalParameter = "What_ever_comes_after_upload"
var new_url = splitArray[0] + "upload/" + additionalParameter + splitArray[1];
alert(new_url);
Let's say I have a input box with the value of "foo_bar"
I want to assign foo to a variable, as well as bar. I never know the lengths of each of those. Essentially, anything to the left of the _ should be one variable, and everything to the right should be another.
How would you do this in jQuery?
You don't need jQuery for that, you can do it in plain JavaScript. A regular expression is one alternative, or if you know that you'll always have the '_' separator, you can use the indexOf method to find its position and split from there.
Or with an example:
var val = $("#inputId").val();
int separatorIndex = val.indexOf('_');
var first = val.substring(0, separatorIndex);
var second = val.substring(separatorIndex + 1);
You could use this code:
var str = "foo_bar"; // Change with input data
var index = str.indexOf("_");
if (index < 0) return false; // Change this with your '_' not found code
var foo = str.substr(0, index);
var bar = str.substr(index + 1);
var a = 'foo_bar';
var b = a.split('_');
var left = b[0];
var right = b[1];
For the sake of variety:
var
str = 'foo_bar',
left = /(.+)_/.exec(str)[1],
right = /_(.+)/.exec(str)[1]
I want to use regular expression in javascript to extract values. The string is in this pattern "title{position}", how should i get title and position in this example?
Assuming that's all there is too it (no nesting of '{}'s or anything) (\w+)\{(\w+)\} will match that instance and group the results as groups 1 and 2. Do you need anything more complicated like a collection of many of these per string or is that enough?
Is that string everything? If so there's no need for matching. Just split the string!
var str = "foo{bar}";
var pieces = str.split(/\{|\}/);
var title = pieces[0];
var position = pieces[1];
alert("Title: " + title + "\nPosition: " + position);
Demonstrated here
If you did want to add results to a structure -
var arr = [];
var text = 'title{position}';
text.replace(/(\w+){(\w+)}/g, function(m, key, value){
//alert('title is-'+key);
//alert('position is-'+value);
arr.push({"title":key,"position":value});
});
console.log(arr);
Demo - http://jsfiddle.net/Sr6Ed/1/
everyone. I've got a string looks like
var s = "2qf/tqg4/ad(d=d,s(f)d)"
And I've got another string
var n = "abc = /fd/dsf/sdf/a.doc, "
What I want to do is insert n after the first '('
So it will look like
"2qf/tqg4/ad(abc = /fd/dsf/sdf/a.doc, d=d,s(f)d)"
Just use the replace function:
var result = s.replace("(", "("+n);
This barely needs REs.
var t = s.replace(/\(/, '('+n);
This doesn't need REs at all, as String.replace takes strings as well as REs to specify what should be replaced.
var t = s.replace('(', '('+n);