I want to ignore spaces in a url parameters. Currently here is my line of script that gets the url parameter of a link.
var paramValue = href.split.('=')[1];
I want to modify it so it ignores any spaces found in the url parameter
Just remove the spaces.
var paramValue = href.split('=')[1].replace(/ /g, '');
Related
So I currently pass two variables into the url for use on another page. I get the last variable (ie #12345) with location.hash. Then from the other part of the url (john%20jacob%202) all I need is the '2'. I've got it working but feel there must be a cleaner and succinct way to handle this. The (john%20jacob%202) will change all the time to have different string lengths.
url: http://localhost/index.html?john%20jacob%202?#12345
<script>
var hashUrl = location.hash.replace("?","");
// function here to use this data
var fullUrl = window.location.href;
var urlSplit = fullUrl.split('?');
var justName = urlSplit[1];
var nameSplit = justName.split('%20');
var justNumber = nameSplit[2];
// function here to use this data
</script>
A really quick one-liner could be something like:
let url = 'http://localhost/index.html?john%20jacob%202?#12345';
url.split('?')[1].split('').pop();
// returns '2'
How about something like
decodeURI(window.location.search).replace(/\D/g, '')
Since your window.location.search is URI encoded we start by decoding it. Then replace everything that is not a number with nothing. For your particular URL it will return 2
Edit for clarity:
Your example location http://localhost/index.html?john%20jacob%202?#12345 consists of several parts, but the interesting one here is the part after the ? and before the #.
In Javascript this interesting part, the query string (or search), is available through window.location.search. For your specific location window.location.search will return ?john%20jacob%202?.
The %20 is a URI encoded space. To decode (ie. remove) all the URI encodings I first run the search string through the decodeURI function. Then I replace everything that is not a number in that string with an empty string using a regular expression.
The regular expression /\D/ matches any character that is not a number, and the g is a modifier specifying that I want to match everything (not just stop after the first match), resulting in 2.
If you know you are always after a tag, you could replace everything up until the "#"
url.replace(/^.+#/, '');
Alternatively, this regex will match the last numbers in your URL:
url.match(/(?<=\D)\d+$/);
//(positive look behind for any non-digit) one more digits until the end of the string
I need to remove part of url if it matches //videoid?v=
assuming my url can be
- www.abc.com/video/
- www.abc.com/video/videoid?v=1234567
- www.abc.com/video//videoid?v=1234567
in case url has // forward slash before videoid?v= then i need to remove single / from the url so that url will be correct such as www.abc.com/video//videoid?v=1234567
currentURL = document.URL;
You can use regex and use it like this to remove double occurrences of "/":
"www.abc.com/video//videoid?v=1234567".replace(/([^:]\/)\/+/g, "$1");
Working example: https://jsfiddle.net/os5yypqm/3/
EDIT:
I edited the JSFiddle to include "http://" in front of the url so that you can see that this does not affect it (and also read it here). Can't use it on SO though so you need to see the fiddle.
If your only concern is the double slash.
var currentUrl = 'www.abc.com/video//videoid?v=1234567';
currentUrl.split('//').join('/');
Results in www.abc.com/video/videoid?v=12345678
You can replace forward slash if followed by forward slash and "v" with empty string
"www.abc.com/video//videoid?v=1234567".replace(/\/(?=\/v)/g, "")
I have a URL e.g. http://localhost:8000/#Test/Method
I am doing following:
System.Windows.Browser.HtmlPage.Window.CurrentBookmark = string.Empty;
which just remove Test/Method but not '#'
I need to modify browser URL
as:
http://localhost:8000/
Any ideas on how to fix this?
If you are manipulating a String you can use the following method :
String Url = "Foo#Bar";
Url = Url.Replace("#", string.Empty);
Use regular expression
String url = "http://localhost:8000/#Test/Method"
url = url.replace(/#/g, "");
The above reg expression will scan for all occurrences of '#' character and replaces with empty character
I have a link like this:
http://nl.wikipedia.org/wiki/Alexandra_Stan&sa=U&ei=UULHUIIdzPnhBOKMgPgJ&ved=0CCIQFjAA&usg=AFQjCNGyCikDkoZMnnuqGo6vjMQ6b5lZkw
I would like to get rid of everything starting at '&' So this will give me a clean url:
http://nl.wikipedia.org/wiki/Alexandra_Stan
I know how to replace href like this:
$('a').each(function() {
$(this).attr("href", function(index, old) {
return old.replace("something", "something else");
});
});
But I can't figure out how to get rid of everything starting at a certain character.
You can use substr() and indexOf() to get a specific portion of the URL, from the beginning of the URL string up until the point the first ampersand is encountered.
var href = $(this).attr('href');
var url = href.substr(0, href.indexOf('&'));
Use String.prototype.split instead. It splits a string by character into an array. The most important part is that if that character is missing (in your case, '&'), it will put the entire string in the first array index anyway.
// String.prototype.indexOf:
var href = 'http://www.noAmpersandHere.com/',
url = href.substr(0, href.indexOf('&')); // ''
// String.prototype.split:
var href = 'http://www.noAmpersandHere.com/',
url = href.split('&'); // ['http://www.noAmpersandHere.com/']
url = url[0]; // 'http://www.noAmpersandHere.com/'
First: consider that the parameters list starts with ? and not with &
The anchor element you are handling already have the entire url parsed and correctly divided.
You need only to access to the correct anchor property:
https://developer.mozilla.org/en-US/docs/DOM/HTMLAnchorElement
why this regular expression replacement doesnt work?
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace('/page\/[0-9]+/', 'page/2'); //it must become http://myweb.com/page/2/id/2
You need to do two things:
change str.replace to url.replace
remove the ' around the regex
var url = 'http://myweb.com/page/1/id/2';
newUrl = url.replace(/page\/[0-9]+/, 'page/2');
Example: http://jsfiddle.net/fhqXn/
Use url rather than str, if you want to replace something in the String stored in the url variable.
You have a naming misspell.
Rename your url var to str or change the str.replace to url.replace:
newUrl = url.replace('/page\/[0-9]+/', 'page/2');