Remove characters from url hash - javascript

Wanting to remove characters from a hash in a url
side bar create url with anchor
e.g
html/g_later_life_lett.html#3.-what-is-important?everything!
var test = window.location.hash;
$(test).replace('?', '')
so when page loads it looks af any ? and ! in hash and removes them.
thanks for help
Updated: thanks add this works fine now
var currentHash = window.location.hash;
var cleanHash = currentHash.replace(/[?!]/g, "");
window.location.hash = cleanHash;

You don't need to use jQuery. you can do this with JavaScript string replace method.
var test = "html/g_later_life_lett.html#3.-what-is-important**?-everything!**";
test = test.replace(/[?!]/g, "")
console.log(test);
The regular expression /[?!]/g selects all ? and ! from the input string.
g: stands for global. And then I am replacing all occurrences with empty string.

this will remove all the ? and ! in a string:
let str = "html/g_later_life_lett.html#3.-what-is-important?everything!"
console.log(str.replace(/[?!]/g,''));

I think you might be looking for this:
var test = window.location.hash;
var newTest = $(test).replace(/(\?|!)/gm, '');
Put the two / marks in to use a regular expression instead of simply a simple string search.
You can also test your regex here: https://regex101.com/.

Related

Regex returns string undefined

I am trying to extract hash value from an magnet link but it returns undefined
var tesst = "magnet:?xt=urn:btih:2B78EDFDDC87DC9605FB285997A80B787888C194&"
var test = tesst.match(/magnet:\?xt=urn:btih:[a-z\d]{40}\&/im);
alert (test[1]);
I cant understand what I am doing wrong.
var test = tesst.match(/magnet:\?xt=urn:btih:([a-z\d]{40})\&/im);
You forgot the ( ) around the hash part.
just mark what you want with capturing group:
/^magnet:\?xt=urn:btih:([a-z\d]{40})\&$/im
Also I recomend to not use regexp here.
Try followed:
tesst.split(':')[3].slice(0, -1);
slice(0, -1) used for remove last '&', you can use any other method, like slice(0, 40), replace(/[^\w]/g, '') or any other.
You need to include [a-z\d]{40} part inside a capturing group and you don't need to escape & symbol, because it isn't a regex meta character.
> var test = tesst.match(/magnet:\?xt=urn:btih:([a-z\d]{40})&/im);
undefined
> console.log(test[1])
2B78EDFDDC87DC9605FB285997A80B787888C194
You can use this regex
/([^:]+)&$/
and use test[1]
console.log(str.match(/([^:]+)&$/)[1]);

remove all empty values from url string

I'm trying to remove all empty params from a url string. My url looks like this
http://localhost/wm/frontend/www/?test=&lol=1&boo=2
my code should return
http://localhost/wm/frontend/www/?lol=1&boo=2
but it doesn't instead it returns
http://localhost/wm/frontend/www/?&lol=1&boo=2
This is the regex i'm using replace("/(&?\w+=((?=$)|(?=&)))/g","") i know i could just use replace() strings that match '?&' after the 1st replace, but i would rather edit my regex to do so, so it's in 1 line of code. Any ideas?
here is my jsfiddle
You can use this regex for replacement:
/[^?&=]+=(?:&|$)|&[^?&=]+=(?=&|$)/g
And replace it by:
""
RegEx Demo
Try
/\w+=&|&\w+=$/g,
var url = "http://localhost/wm/frontend/www/?test=&lol=1&boo=2&j=";
document.write(url.replace(/\w+=&|&\w+=$/g, ""))
Just try to invoke native's 'replace', which could be used with a regex in its first argument.
str.replace(regex, replace_str)
Please, see this fiddle to see a running example: http://jsfiddle.net/xvqasgmu/1/
You can for example say:
var s = 'http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=';
s = s.replace(/\w*=\&/g, '');
s = s.replace(/&\w*=$/g, '');
That is, remove a block of letters + = + &. Then, remove & + letters + = at the end of the line (indicated by $).
For your input, it returns:
http://localhost/wm/frontend/www/?lol=1&boo=2
See it in JSFiddle or directly here:
var s = 'http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=';
s = s.replace(/\w*=\&/g, '');
s = s.replace(/&\w*=$/g, '');
document.write(s)
Test
If the input contains blocks in the middle and in the end:
http://localhost/wm/frontend/www/?test=&lol=1&boo=2&r=
the code I wrote above returns:
http://localhost/wm/frontend/www/?lol=1&boo=2

Javascript replace one or another

I'm trying to make the replace function work when having one match or another. It's very simple as a logic so I'd like to have a very simple implementation.
I have tried:
var my_url = document.URL;
var tmpl = "?tmpl=component" || "&tmpl=component"; //This is the tricky part
location.href = my_url.replace(tmpl,"");
...but it doesn't seem to work. Any ideas please?
This is not how JavaScript works, logical OR is useless here. One possible way is using regex:
location.href = my_url.replace(/[?&]tmpl=component/, "");
Here the replace method will replace any match of tmpl=component starting with either ? or &.
You're setting tmpl to be the value of the expression "?tmpl=component" || "&tmpl=component";, which will always evaluate to "?tmpl=component", since it is the first truthy value in your or statement.
You can do this with regex in a number of ways:
my_url.replace(/?tmpl=component|&tmpl=component/, "");
my_url.replace(/[?&]tmpl=component/, "");
You could do two replacements:
location.href = my_url.replace("?tmpl=component", "").replace("&tmpl=component", "");
or you could use a regular expression: (recommended)
location.href = my_url.replace(/[?&]tmpl=component/, "");
[?&] will match either a '?' or '&' character.
Best one is:
var tmpl = (my_url.indexOf("?tmpl=component") > -1)? "?tmpl=component" : "&tmpl=component";

remove last two parameters from URL

i want to break a following url
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324
into this by eliminating last two parametes i.e. &cn=bella&cjid=3344324
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434
the length of the url may change but the last two parameters remains in that position only. so how can i remove that in a efficient way.
A RegExp is the easiest way for this case:
str = str.replace(/&[^&]*&[^&]*$/,'');
You can use replace with regular expression. If the url is in var url then you can use this one
var new_url = url.replace(/&cn=.*/, '');
you can test it with
var url = 'http:\www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324';
console.info(url.replace(/&cn=.*/, ''));
var string = "http://dom.com/?one=1&two=2&three=3&four=4";
string.match(/(.*)&(.*)&(.*)/)[1]; // strips last two parameters
You can use regular expressions to replace the last 2 parameters with the empty string:
var url = "http://www.example.com/?p1=1&p2=2&p3=3&p4=4";
var urlWithoutLast2Parameters = url.replace(/&[^&]+&[^&]+$/,"");
You could use the function IndexOf to find the location of the '&cn' and then just use the substring function to create a new string eliminating the '&cn' portion of the URL, so something like...
var intIndexOf = str.IndexOf('&cn=')
strURL = strURL.substring(0,intCharAt)

Simple Regex question

I want to remove this from a url string
http://.....?page=1
I know this doesn't work, but I was wondering how you would do this properly.
document.URL.replace("?page=[0-9]", "")
Thanks
It seems like you want to get rid of the protocol and the querystring. So how about just concatenating the remaining parts?
var loc = window.location;
var str = loc.host + loc.pathname + loc.hash;
http://jsfiddle.net/9Ng3Z/
I'm not entirely certain what the requirements are, but this fairly simple regex works.
loc.replace(/https?\:\/\/([^?]+)(\?|$)/,'$1');
It may be a naive implementation, but give it a try and see if it fits your need.
http://jsfiddle.net/9Ng3Z/1/
? is a regex special character. You need to escape it for a literal ?. Also use regular expression literals.
document.URL.replace(/\?page=[0-9]/, "")
The answer from #patrick dw is most practical but if you're really curious about a regular expression solution then here is what I would do:
var trimUrl = function(s) {
var r=/^http:\/\/(.*?)\?page=\d+.*$/, m=(""+s).match(r);
return (m) ? m[1] : s;
}
trimUrl('http://foo.com/?page=123'); // => "foo.com/"
trimUrl('http://foo.com:8080/bar/?page=123'); // => "foo.com:8080/bar/"
trimUrl('foobar'); // => "foobar"
You're super close. To grab the URL use location.href and make sure to escape the question mark.
var URL = location.href.replace("\?page=[0-9]", "");
location.href = URL; // and redirect if that's what you intend to do
You can also strip all query string parameters:
var URL = location.href.replace("\?.*", "");

Categories

Resources