This question already has answers here:
Getting parts of a URL with JavaScript
(5 answers)
Closed 5 years ago.
url:
http://xxxxxx.com/video/view/12345
Can I take 12345 in the url using javascript?
Please help me
Use RegExp, Array#match and negative lookahead.
var str = 'http://xxxxxx.com/video/view/12345';
console.log(str.match(/(?!view\/)\d+/)[0]);
You can also try this if you're sure that it'll always be in last:
var num = location.pathname.split('/').pop(); // "12345"
and further: parseInt(num);
You can parse your URL with the following code. Then just get the last part.
var url = 'http://xxxxxx.com/video/view/12345';
var url_parts = url.replace(/\/\s*$/,'').split('/');
console.log(url_parts[url_parts.length - 1]); // last part
Related
This question already has answers here:
Get the values from the "GET" parameters (JavaScript) [duplicate]
(63 answers)
Closed 5 years ago.
I have a string (which is the value of href attribute) like this:
var url = '/myweb/search?s=islamic_sources&q=test';
It also sometimes looks like these:
url = '/myweb/search?q=test';
url = '/myweb/search?q=test&g=sth';
Here
^[^?]+.*?&?q=([^$|&]+)
is what I've tried so far, and I need to match test in those three lines. But sadly seems $ doesn't mean end of line in my pattern. How can I fix it?
You can simply use:
url.match(/q=([^&]*)/)
This will return an array. If not empty, test will be at index 1.
var url1 = '/myweb/search?s=islamic_sources&q=test';
var url2 = '/myweb/search?q=test';
var url3 = '/myweb/search?q=test&g=sth';
console.log(url1.match(/q=([^&]*)/)[1]);
console.log(url2.match(/q=([^&]*)/)[1]);
console.log(url3.match(/q=([^&]*)/)[1]);
the regex is /q=[^&]+/ and execute it in the url, then take first match, split it on =, then take second part.
I created it as a function below.
var url1 = '/myweb/search?s=islamic_sources&q=test';
var url2 = '/myweb/search?q=test';
var url3 = '/myweb/search?q=test&g=sth';
function retMatch(url){
var re=/q=[^&]+/;
return re.exec(url1)[0].split('=')[1];
}
console.log(retMatch(url1));
console.log(retMatch(url2));
console.log(retMatch(url3));
This question already has answers here:
js function to get filename from url
(24 answers)
Closed 6 years ago.
Guys i have many links:
"img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png",
How in js change sting to last "/"? To get just the name of image?
var url = "img/dasda/ja.png";
var idx = url.lastIndexOf('/');
console.log(url.substring(idx + 1));
You could use Array#match together with RegExp.
var links = ["img/dasda/ja.png", "../img/no.png", "wild/rpm/gulp/in.png"];
links.forEach(v => console.log(v.match(/(?!\/)\w+(?=\.)/g)[0]));
This question already has answers here:
How can I delete a query string parameter in JavaScript?
(27 answers)
Closed 6 years ago.
i have a url like this
test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14
i want to remove the parameter using the following javascript
function removeParam(uri) {
uri = uri.replace(/([&\?]start_date=*$|start_date=*&|[?&]start_date=(?=#))/, '');
return uri.replace(/([&\?]end_date=*$|end_date=*&|[?&]end_date=(?=#))/, '');
}
but it didn't work, anyone know what's wrong with that?
in modern browsers you can do this quite simply
var x = new URL(location.origin + '/test.html?dir=asc&end_date=2016-09-23&order=created_at&start_date=2016-08-14');
x.searchParams.delete('start_date');
x.searchParams.delete('end_date');
var uri = x.pathname.substr(1) + x.search; // substr(1) because you don't have a leading / in your original uri
at least, I think it's simpler
Your RegExp is no match!
If you want remove end_date, you should:
uri.replace(/(end_date=)([^*&]+)/, 'end_date=')
And so on.
This question already has answers here:
Remove querystring from URL
(11 answers)
Closed 7 years ago.
How would I remove part of a URL with jQuery, when the said part is changeable?
E.g. how can I remove this from my URL: ?modal=name
when 'name' might be name1 name2 or name3
I guess I will need to split the URL, but how do I do this when the modal name might be anything?
var url = 'http://yourdomain.com/search?modal=name';
alert(url.substring(0, url.indexOf('?')));
Demo
As you have asked "How to remove the part of a URL" using jQuery
Here is a basic workaround for you:
//the URL ( decode it just for GOOD practice)
var someRandomUrl = decodeURI("http://example.com?modal=name");
//here we do the splitting
var splittedParts = someRandomUrl.split("?");
// the first part of the array will be URL you will require
var theURL = splittedParts[0];
// the second part of the array will be the Query String
var theQuery = splittedParts[1];
alert(theURL);
This question already has answers here:
endsWith in JavaScript
(30 answers)
Closed 9 years ago.
Is there a javascript function to get the last occurrence of a substring in a String
Like:
var url = "/home/doc/some-user-project/project";
I would like a function that returns true if the String contains project at his end.
I know str.indexOf() or str.lastIndexOf() but is there another function that do the job or should I do it?
Thanks for the answer
Something like
var check = "project",
url = "/home/doc/some-user-project/project";
if (url.substr(-check.length) == check){
// it ends with it..
}
Try this
<script>
var url = "/home/doc/some-user-project/project";
url.match(/project$/);
</script>
The response is a array with project, if the responde is 'null' because it is not found