I have some urls and I want to add the string ".webp" in the alphanumeric id just before the parameter &size
http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102
http://r.rp-static.pre/r/dsn-icon?dsn=caucp0d8ig8o&size=g&v=20210309074045
http://r.rp-static.pre/r/dsn-icon?dsn=9guq9t6e5fs1&size=g&v=20210318114201
I only get these strings so I have to add ".webp" somehow with javascript.
I want this result:
http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g.webp&size=g&v=20210314224102
http://r.rp-static.pre/r/dsn-icon?dsn=caucp0d8ig8o.webp&size=g&v=20210309074045
http://r.rp-static.pre/r/dsn-icon?dsn=9guq9t6e5fs1.webp&size=g&v=20210318114201
how can I do it? Thanks
let temp = "http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102"
console.log(temp.slice(0, temp.indexOf("&size")) + ".webp" + temp.slice(temp.indexOf("&size")))
You can store the url first in a variable and then use slice to update the url.
console.log('http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g&size=g&v=20210314224102'.replace(/&size/, '.webap&size'))
let a ='http://r.rp-static.pre/r/dsn-icondsn=xjfhob38jg8g&size=g&v=20210314224102'
//your url
let url = a.replace('&size', '.webp&size');
//new url 'http://r.rp-static.pre/r/dsn-icon?dsn=xjfhob38jg8g.webp&size=g&v=20210314224102'
Related
I am having a little problem. I want to get the file name
(横 浜 プ レ _ 図 面 デ ー タ .pdf)
from the url below. I don't know how to use regex to find the file name.
Hope everybody help please.
https://reclaim-files-dev.s3.ap-northeast-1.amazonaws.com/attachment/横浜プレ_図面データ.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIASUJZK2B4ZLI77WWZ%2F20200303%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Date=20200303T042736Z&X-Amz-Expires=300&X-Amz-Signature=b8b00cb04dbe5a73de8230327651636784a0c9d7979a5666e13b54d67f116703&X-Amz-SignedHeaders=host
Create an URL and let it parse:
const url = new URL(yourUrlString);
Filter the file name:
const fileName = url.pathname.replace(/^.*[\\\/]/, "");
If the filename always follows /attachment/ in the URL, and presumably is always followed with the ? and the rest of the parameters:
Assuming the entire url is assigned to urlString
let startIndex = urlString.indexOf("/attachment/");
let endIndex = urlString.indexOf("?");
let fileName = urlString.substring(startIndex, fileName);
This will find any filename regardless of file type (you mention pdf, but this would find files with other extensions).
var url = 'https://reclaim-files-dev.s3.ap-northeast-1.amazonaws.com/attachment/横浜プレ_図面データ.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIASUJZK2B4ZLI77WWZ%2F20200303%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Date=20200303T042736Z&X-Amz-Expires=300&X-Amz-Signature=b8b00cb04dbe5a73de8230327651636784a0c9d7979a5666e13b54d67f116703&X-Amz-SignedHeaders=host';
var url2 = url.substring(0, url.indexOf('?X'));
console.log(url2.substring(url2.lastIndexOf('/') + 1));
I guess you can do this without regex
x = "https://reclaim-files-dev.s3.ap-northeast-1.amazonaws.com/attachment/横浜プレ_図面データ.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIASUJZK2B4ZLI77WWZ%2F20200303%2Fap-northeast-1%2Fs3%2Faws4_request&X-Amz-Date=20200303T042736Z&X-Amz-Expires=300&X-Amz-Signature=b8b00cb04dbe5a73de8230327651636784a0c9d7979a5666e13b54d67f116703&X-Amz-SignedHeaders=host"
y = x.split('/')
["https:", "", "reclaim-files-dev.s3.ap-northeast-1.amazonaws.com", "attachment", "横浜プレ_図面データ.pdf?X-Amz-Algorithm=AWS4-HMAC-SHA256&…979a5666e13b54d67f116703&X-Amz-SignedHeaders=host"]
your attachment will be in the 4th index
str = y[4].substr(0, y[4].indexOf('.pdf'))
If you are looking to get the file name from specifically this arrangement of a URL, then simply split by the first argument (?) and then split by forward slash. This will give you an array which you can reverse. The first element of this array will be your file name:
function get_file(url_string){
return url_string.split('?')[0].split('/').reverse()[0];
}
If you want to go a step further, you can then split the result by (.) in order to get the actual name and the file extension in a single step:
function get_file(url_string){
return url_string.split('?')[0].split('/').reverse()[0].split('.');
}
Note: as previously mentioned, this only works with your URL arrangement.
I have a URL which will be something like below
http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg
I want URL which will be something like this
NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg
Here is what I get in variable
VSATSaving.PANAROMIC_120 = document.getElementById('ImgPanaromic120').src;
how to get that using javascript. Tried with lastIndexOf but it is not working
You can create a new URL object and use the pathname property to extract the data.
const myUrl = new URL(document.getElementById('ImgPanaromic120').src);
console.log(myUrl.pathname);
<img id="ImgPanaromic120" src="http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg"/>
var a = document.createElement('a');
a.href = 'http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg';
console.log(a.pathname);
window.location.pathname will give you path.
You can simply use window.location.pathname since this is a url.
For other strings, you can use indexOf, split, substring and many of the other string functions.
//example using `split`
var str = "http://localhost:22306/NESSPATH/VSAT/I-HP-AAMB-ENB-0003_C1//Panaromic//120.jpg";
var paths = str.split("http://localhost:22306/")
console.log(paths[1]);
I have a file path as shown below.The last part (i.e. video2.mp4) is dynamically changed.I'm using Javascript/Typescript.
file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4
Question: How to get the file:///data/user/0/com.sleep.app/files/sleep-videos/ part only from above string.
var string = 'file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4';
var stringPart = string.substring(0,string.lastIndexOf("/")+1);
If only the filename changes try something like here
^(.+)/([^/]+)$
Try it at regexr. Your first group is the dirname, the second group the basename.
If you have further requirements/information (e.g. which tools you use), please refine your question.
var url = "file:///data/user/0/com.sleep.app/files/sleep-videos/video2.mp4";
url= url.split("/");
url.pop();
url = url.join("/");
What I'm trying to do is fetch a single piece of a string without using the hashtag element in the url. I already have a functioning code but it needs altering. So, how do I fetch any part of the url after ?.
Say I have ?fx=shipment+toys/fish-fix-fx/ as my url string; I want the button to show if shipment or fish or fx was my choice of selections for example.
Buttons showing with hastag: http://jsfiddle.net/66kCf/2/show/#iphone
Original JSFiddle (buttons not showing): http://jsfiddle.net/66kCf/2/
I want the iPhone buttons to show if fix was my choice: http://jsfiddle.net/66kCf/2/show/?fx=shipment+toys/fish-fix-fx/
try doing it with .split() and.match() like this...
var keys = window.location.href.split('?');
if (keys[1].match(/(fix|fish|fx)/))
{
$("#linkdiv").append(nextLink);
$("#linkdiv1").append(nextLink);
$("#linkdiv2").append(nextLink);
}
demo button showing : http://jsfiddle.net/LbKmf/show/?fx=shipment+toys/fish-fix-fx/
demo button not showing: http://jsfiddle.net/LbKmf/show/?reigel
Is this what your looking for:
"?fx=shipment+toys/fish-fix-fx/".split(/[\?=+\/-]/g);
window.location.search and split into array for comparisons
explained in How can I get a specific parameter from location.search?
http://css-tricks.com/snippets/javascript/get-url-and-url-parts-in-javascript/
Generally, Javascript doesn't have a built-in functionality for query string parameters. You can use string manipulation on window.location.search to get your parameters out of the URL string. Note that location.search includes the ? character too.
Something like this should do:
var queryString = function () {
// Anonymous function - executed immediately
// get rid of the '?' char
var str = "?fx=shipment+toys/fish-fix-fx/";
var query = str.substring(str.lastIndexOf('=')+1,str.indexOf('/'));
var vars = query.split("+");
for (var i=0;i<vars.length;i++){
console.log(vars[i]);
}
return vars;
} ();
i have an URL like the followin,
http://test.com/testing/test/12345
12345 is the id. I want to take this using query string. How to take this value in javascript?
try like this
http://test.com/testing/test/12345
var aarr = window.location.href.split('/');
//get last value
var id = aarr[aarr.length -1];
or just
var id = window.location.href.split('/').pop()
Use this :
document.location.href.split('/').pop()
Running it on this page yields : 22139563#22139563
Use this code:
var id = location.split('/').pop();
That's part of the path, not the query string... but you can access the page's URL using window.location.
The path is available at window.location.pathname which can be split up using forward slashes: window.location.pathname.split('/')
And then you can get the last item of the array: window.location.pathname.split('/').pop()
I would use substring, I think it's lighter than creating an array:
var id = window.location.href;
id = id.substring(id.lastIndexOf('/')+1);