How to get the id of this URL :
http://domainpertest.tk/reset/11E89887FABBC1D
So when I click this link, I want to get this 11E89887FABBC1D
Any idea please?
try to use javascript split method for getting this id.
var str = "http://domainpertest.tk/reset/11E89887FABBC1D";
var res = str.split("/");
console.log(res[res.length-1]);
You can use regex or extract substring with url.substring(url.lastIndexOf("/"), url.length-1)
Try this:
function findIdInUrl() {
var url="http://domainpertest.tk/reset/11E89887FABBC1D";
return url.slice(url.lastIndexOf('/')+1);
}
Related
I have following url as a string:
var fb= "https://www.facebook.com/XYZ"
I want to remove https://www.facebook.com and display only /XYZ.
I tried alert(fb.replace("facebook.com","")); alert(fb.substring('facebook.com'.length));
but it didn`t help
Tried code :
alert(fb.replace("facebook.com",""));
alert(fb.substring('facebook.com'.length));
Any help will be appreciated.
Looks like you are trying to get the last part of the given url.
This will do.
alert(fb.substr(fb.lastIndexOf('/')));
Demo
This is simple. Try the following:
var fb="https://www.facebook.com/XYZ";
var x = fb.substr(fb.lastIndexOf("/"), fb.length);
Now x has got the final result in x as
"/XYZ"
Try this.
var fb = https://www.facebook.com/XYZ
fb = fb .substr(fb.indexOf("/") + 1); // Contains XYZ //
Use String#replace and a regular expression (which allows you to add flexibility in the kind of URLs you accept, like http / https or www.facebook.com / facebook.com).
var fb = 'https://www.facebook.com/XYZ'
console.log(
fb.replace(/^https?:\/\/(?:www.)?facebook.com(?=\/)/, '') //=> '/XYZ'
)
<script type="text/javascript">
var fb="https://www.facebook.com/XYZ"
fb.split('/')[3];
alert(fb.split('/')[3])
</script>
it working please try
var fb="https://www.facebook.com/XYZ";
var res = fb.replace("https://www.facebook.com", "");
I have an array with one element
ClickMeArray[0] = "ClickMe=a-6,a-7,a-8,a-9,"
I want variable to return elements after ClickMe= from ClickMeArray.
Output should be a-6,a-7,a-8,a-9,.
ClickMeArray[0].split('ClickMe=')[1];
Try This
var val = "ClickMe=a-6,a-7,a-8,a-9,";
var myString = val.substr(val.indexOf("=") + 1);
There are many way to do this work. One way is using .replace(). You can replace ClickMe= with empty to getting another part of string.
var ClickMeArray = "ClickMe=a-6,a-7,a-8,a-9,";
console.log(ClickMeArray.replace('ClickMe=',''));
I have the following string, "blahblahhellothere", that I would like to be shortened to "hellothere" using JavaScript and/or JQuery.
I have tried using the following code:
var titletext123 = "blahblah<br>hellothere"
var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") +1);
Which only returns "br>hellothere".
Does anyone have any code which will get rid of the and all text before it?
Thank you very much. All of your help is appreciated!
Make it
var titletext123 = "blahblah<br>hellothere" var injuryt3xt = titletext123.substring(titletext123.indexOf("<br>") + 4);
So it is +4. Which accounts for all the characters in <br>.
You can use split() and get second element.
var titletext123 = "blahblah<br>hellothere" ;
var injuryt3xt = titletext123.split("<br>")[1];
alert(injuryt3xt);
Using regular expression:
var text = "blahblah<br>hellothere"
var clipped = str.replace(/.+\<br\>/, ""));
Another option (depending on circumstances) might be:
var injuryt3xt = titletext123.split("<br>")[1];
Which would split the string on <br> and return an array with the left-over parts ... the second of which is referred to with the [1]
I want to remove the hash, as well as anything after it, from a URL. For example, I might have:
http://example.com/#question_1
… which slides to question no. 1 to show an error message. When the user’s input then passes validation, I need to remove #question_1 from the current location.
I’ve tried all of these, but none of them has worked for me:
document.location.href.replace(location.hash, "");
window.location.hash.split('#')[0];
window.location.hash.substr(0, window.location.hash.indexOf('#'));
Note: I don’t just want to get the URL – I want to remove it from my address bar.
history.pushState("", document.title, window.location.href.replace(/\#(.+)/, '').replace(/http(s?)\:\/\/([^\/]+)/, '') )
Try this :use .split() to split string by # and then read first element in array using index 0
var url = 'http://example.com#question_1';
var urlWithoutHash = url.split('#')[0];
alert(urlWithoutHash );
Use split in javascript
var str = "http://example.com#question_1";
alert(str.split("#")[0]);
Try this way:
var currentPath = window.location.pathname;
var myUrl = currentPath.split("#")[0];
OR
var currentPath = window.location.href;
var myUrl = currentPath.split("#")[0];
Hope it helps.
This will clear the id selector from the uri
location.hash = '';
Use .split as shown :
var str = "http://example.com#question_1";
alert((str.split("#")[0]);
or use .substring() as shown :
var str = "http://example.com#question_1";
alert((str.substring(0,str.indexOf('#'))));
I would like to extract the following String :
http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
from this String :
https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg
And before, extract, i would like to check if the global String contains more than one time "http" to be sure to extract the jpg only when needed.
How can i do that ?
Extract the data like this:
var myStr = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var splittedStr = myStr.split("-");
var extractedStr = splittedStr[3].slice(1);
To find out how many "http" is present in the string:
var count = (myStr.match(/http/g)).length;
Hopes it helps
var source = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg"
var temp = source.replace("https","http").split("http");
var result = 'http'+temp[2];
use split()
var original = "https://s1.yimg.com/bt/api/res/1.2/yqEp3ogcVvfSaDSSIq.Llg--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3E9NzU7dz0xMzA-/http://media.zenfs.com/fr_FR/News/AFP/a418cb581c41fd9c36b0d24c054ad4c623bab222.jpg";
original = original.split('-/');
alert($(original)[original.length-1]);
your require URL shows in alert dialog
You can use regex :
str.match(/(http?:\/\/.*\.(?:png|jpg))/i)
FIDDLE