Unable to replace characters in javascript - javascript

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", "");

Related

How to get ID from link, using Nativescript

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);
}

Replace %20 by a space in a 'url.split' in javascript

I know that this question has been asked more than once here. But the issue I'm facing is a bit different that the one's I checked in previous questions.
I'm retrieving the Category in a variable from the URL in my JS by doing this-
$scope.category = url.split("=")[1]
This would be a sample URL-
sitename/pagename.aspx?Category=Cars
I'm facing an issue when there is a space in the category. Say if the URL is like this-
sitename/pagename.aspx?Category=Super%20Cars
Can you please lemme know how to replace the %20 by a space along with the url.split.
Sure this isnt a right one-
$scope.category = decodeURIComponent(url.split("=")[1]);
Lemme know if you need more info guys. :)
You can try this:
url.split('%20').join(' ');
You can do it by below syntax:
var url = window.location.href;
var tempUrl = url.replace(/%20/g," ");
$scope.category = tempUrl.split("=")[1]
You need to use decodeURI to achieve this.
http://www.w3schools.com/jsref/jsref_decodeuri.asp
Use decodeURI global function available in javascript. http://www.w3schools.com/jsref/jsref_decodeuri.asp
var url="http://www.dummyserver.com/index.php?param1=hello%20world";
var queryParams=url.substring(url.indexOf("?") + 1).split("&");
for(i=0;i<queryParams.length;i++){
var splitParam = queryParams[i].split("=");
alert("Param: " + splitParam[0] + " :: Value:" + decodeURI(splitParam[1]));
}
You can convert the url into string first and then replace %20 with the space and then split,it should work,can try like this:
var str = 'sitename/pagename.aspx?Category=Super%20Cars';
var res = str.toString();
res = res.replace('%20',' ');
category = decodeURIComponent(res.split("=")[1]);

How To Remove All Text Before a Specific Value in a String

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]

Remove hash from current page’s URL

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('#'))));

Regex javascript + replace

I got few lines like these:
/coc59409.p?id=1218405784092
/acme-made-skinny-lray-orange/5616664.p?id=1218679205878
/incase-campus-brk-gray-pink-berry/7209107.p?id=1218833962192&skuId=7209107
and I need to delete in all of them what come after this ".p?id="
how can I do that?
results :
/coc59409.p?id=
/acme-made-skinny-lray-orange/5616664.p?id=
/incase-campus-brk-gray-pink-berry/7209107.p?id=
=> "/coc59409.p?id=1218405784092".replace(/\?.*$/,"")
>> "/coc59409.p"
You don't need a regular expression here - contextually what you are referring to is the query string of the URL and there are already tools in the DOM with the facility to tokenise this, for example:
var url = '/coc59409.p?id=1218405784092',
a = document.createElement('a');
a.href = url;
console.log( a.pathname ); // '/coc59409.p'
Details about the HTMLAnchorElement can be found here.
You could just split it,
var url = '/acme-made-skinny-lray-orange/5616664.p?id=1218679205878'
var urlArr = url.split('=');
console.log(urlArr[0]+'=');
Hope that helps :)
Take each line and replace id=[THE REST OF THE LINE] with id=.
Like so:
"coc59409.p?id=1218405784092".replace(/id=(.*)$/, 'id=')

Categories

Resources