With string http://www.example.com/section_one/index.html how can I return index using RegExp?
P.S.: Returning index.html is accepted too.
You don't need to use regex for that. Its simple:
var url = document.URL;
var currentPage= url.split("/").pop();
You can try the following.
Fiddle
var url = "http://www.example.com/section_one/index.html";
var filename = url.match(/[^\\/]+$/)[0];
Related
I got a link lie this
url = 'http//mysite.com/product/id/122?u=12'
I want to turn that to this
'http//mysite.com'
remove all the uri and query string
How you guys do it?
You can use URL api
let urlParsed = new URL("http://example.com/product/id/122?u=12")
let {origin} = urlParsed
console.log(origin)
Use split and string concatenation
var url = 'http//mysite.com/product/id/122?u=12'
console.log(url.split('.com/')[0]+'.com')
I'm a newbie in web development so pls forgive my newbie question.
I have a URL "https://123asd.my.website.com/blabla/blabla/blabla
What I'm trying to figure out is how do I get the "123asd" so that I can set in on my var. Thank you
You can use regex
var url = 'https://123asd.my.website.com/blabla/blabla/blabla';
var number = url.match(/([0-9a-z]{1,})\./)[1];
console.log(number);
const url = "https://123asd.my.website.com/blabla/blabla/blabla";
let firstStr = url.replace("https://", ""); // get rid of "https://" or you can do it by some other way
firstStr = firstStr.substring(0, firstStr.indexOf('.')); // get the substring start from the beginning to the first '.'
console.log(firstStr); // 123asd
var url="https://123asd.my.website.com/blabla/blabla/blabla";
var urlNoHttps=url.replace(/^https?\:\/\//i, "");
var hostName=urlNoHttps.split('.')[0];
console.log(hostName);
The above code works for both http and https protocol.
I need the whole parameter list as such , not one by one
var Url = "http://localhost/Home/Admin?param1=1¶m2=2$param3=3";
I want to get the whole parameter list from the url.
var params = "param1=1¶m2=2¶m3=3";
var Url = "http://localhost/Home/Admin?param1=1¶m2=2$param3=3";
var urlArray = url.split("?");
var params=urlArray[1];
You can see Using split() example of Mozilla Developer Network for more insight on using the split function.
Thanks for the support, I use this one for my need
var params = window.location.href.split('?')[1];
I have an URL e.g http://test.example.com I need to extract test (sub domain of this url.) from this url. This url is dynamic it may change so couldn't match with test and extract it. I have written block of code it is working for me. Can anyone suggest me better way to achieve this.
var siteUrl = 'http://test.example.com';
var parts = siteUrl.split('.');
var subdomainstr = parts.shift(); // Output 'http://test'
var upperleveldomain = parts.join('.'); // Output 'example.com'
var extractSubDomain = subdomainstr.split('//');
var subdomain = extractSubDomain.slice(1).join('.');
console.log(subdomain); //Output test
fiddle
Why not this?
var subdomain = siteUrl.split('//')[1].split('.')[0];
Working demo
If you prefer regex, this works
var subdomain = siteUrl.match(/http:\/\/([^\.]*)/i)[1];
Trying to remove the full url that is being returned to imgurl:
Usually returns something like http://localhost/wordpress/wp-content/uploads/filename.jpg
or http://localhost/wordpress/wp-content/uploads/images/filename.jpg
I'd like to strip off everything except filename.jpg and return it to
ahng_photos_upload_image. Strip off everything to the last forward-slash.
How can I do that with Jquery?
window.send_to_editor = function(html) {
imgurl = jQuery('img',html).attr('src');
jQuery('#ahng_photos_upload_image').val(imgurl);
tb_remove();
}
You don't need jQuery for that, just plain old JavaScript will do :)
alert('http://localhost/wordpress/wp-content/uploads/filename.jpg'.split('/').pop());
In your case:
var filename = imgurl.split('/').pop();
you can use a regular expression in order to achieve this..
var file = imgUrl.replace(/^.*[\\\/]/, '');
Now the file would consist of only the file name ..
If you're pretty confident that the URLs don't have funny stuff like hashes or parameters, a regex like this would do it:
var filename = imgurl.replace(/^.*\/([^/]*)$/, "$1");
Also: don't forget to declare "imgurl" with var, and you should probably use .prop() instead of .attr() if your version of jQuery is 1.6 or newer:
var imgurl = jQuery('img', html).prop('src');
Also jQuery internally turns the two-argument form of the function into this:
var imgurl = jQuery(html).find('img').prop('src');
so you might as well code it that way.
One further option:
var filename = imgurl.substring(imgurl.lastIndexOf('/') + 1);
JS Fiddle demo.
Here you have
var filename = imgurl.split('/').slice(-1);
Good luck!
Try this one:
imgurl.split('/').slice(-1);
Edit: Look at the version of #Andy who uses the pop() method, the latter being faster than slice(-1).
Note that if you don't know if you have forward or backward slashes, you are better off using the RE version of split:
"path".split(/[\/\\]/).slice(-1)
Here is an answer that will work when your file name is like ./file.jpg
var extension = fileName.slice((fileName.lastIndexOf(".") - 1 >>> 0) + 2);
var baseName = fileName.replace(/^.*\/([^/]*)$/, "$1");
var path = fileName.replace(/(^.*\/)([^/]*)$/, "$1");