I need to change URL param By JavaScript or jquery.
My URL is:
domain.com/path/to/reach/5?order%5Bsort%5D=2&c=5
I want change paramt c and order[sort]
like this:
domain.com/?order%5Bsort%5D=3&c=15
domain.com/5?order%5Bsort%5D=2&c=16
domain.com/5?order%5Bsort%5D=1&c=17
my code is this:
regEx = /([?&]p)=([^#&]*)/g;
dataUrl = dataUrl.replace(regEx, '$1='+page);
regEx = /([?&]order[[]column[]])=([^#&]*)/g;
dataUrl = dataUrl.replace(regEx, '$1='+data["order[column]"]);
but don't work!!!!
try this:
let orderSort=4,c=6;
let url='domain.com/path/to/reach/5?order%5Bsort%5D=2&c=5';
let result=url.replace(/order%5Bsort%5D=\d*&c=\d*/,'order%5Bsort%5D='+orderSort+'&c='+c);
console.log(result);//domain.com/path/to/reach/5?order%5Bsort%5D=4&c=6
explanation:
i extract the key you offered and use the value to make a new and target string.
then, replace function works by replace the whole string the reg matches with the target string.
Because of the order of the parameters, it's not possible to use your code. So I used the following code, but the first two lines worked correctly, but the second two lines have problems and add the value to it every time.
dataUrl = dataUrl.replace(/c=\d*/, 'c='+count);
dataUrl = dataUrl.replace(/p=\d*/, 'p='+page);
dataUrl = dataUrl.replace(/order%5Bsort%5D=\d*/, 'order%5Bsort%5D='+dataObj["order[sort]"]); //
dataUrl = dataUrl.replace(/order%5Bcolumn%5D=\d*/, 'order%5Bcolumn%5D='+dataObj["order[column]"]);
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("/");
I have this RegExp: var myReg = RegExp("https?://www.youtube.com/watch\?v=[^\"]+",""); to find the youtube link within a string. I then want to make the part of the string matching the RegExp a variable; lets say var url = "part of string matching the RegExp"
then I coudl do something like window.location = url; to redirect the browser directly to the video page. Thanks
You only have to access the first element of the result, if any:
var r = string.match(myReg);
if(r) var url = r[0];
Take care because is you dont find the url, the result will be a null value
This should do the trick:
myReg.exec(str);
var url = RegExp.lastMatch;
Udi
I have a search function, and would like to display the search term in the search input.
My url is: search-1.html?keyword=XXXXXX
How do I get this, and display it in an input?
Thank you in advance.
Use this:
http://ajaxcssblog.com/jquery/url-read-get-variables/
Take luck!
Oh and then you can use the following to display its value in an input field:
$("#inputId").val($.url.param("keyword"));
If it is just one key=value in the url you can use simple regex like this:
var theValueYouWant = window.location.href.match(/keyword=(.+)/)[1]
And set the value of an input like this
$('input').val(theValueYouWant)
If you want to parse the GET string more thoroughly, this function should do it...
gets = {};
$.each(location.search.replace(/^\?/,'').split('&'),function(k,v){
bits = v.split('=');
gets[bits[0]] = bits[1];
});
Regex solution:
var S = window.location.search; // "?keyword=..." etc.
var T = S.match(/^\?(?:[^\b]*&+)?keyword=([^&]*)/);
if (T)
T = T[1]
else
T = "no keywords found"
If multiple values are given for "keyword" (e.x. ?keyword=XXX&keyword=YYY), the regex will only find the first of these values (e.x. XXX). This regex works even if there are other variables in the query string.
jQuery-less solution:
<script type="text/javascript">
var $_GET=[],pairs=location.href.toString().substring(location.href.toString().indexOf("?")+1).split("&");for(key in pairs){pos=pairs[key].indexOf("=");$_GET[pairs[key].substring(0,pos)]=decodeURIComponent(pairs[key].substring(pos+1).replace(/\+/g," "))};
// Now just access with $_GET
// example...
keyword = $_GET["keyword"];
</script>