How to remove spacific values from url string - javascript

I have this URL string:
http://jackson/search/page/3/?features=Sea%20View&submit=search
Now I want to remove this part from ul: page/3/ when page is reload.
I am not good with jQuery and regex so I will appreciate if you help me.
Note: Page number can be anything form 1 to 100.
Thanks.

Have your tried using String.replace()
var url = "http://jackson/search/page/3/?features=Sea%20View&submit=search";
url = url.replace(/page\/\d+\//, '');

\d match any digit character:
url = 'http://jackson/search/page/3/?features=Sea%20View&submit=search';
url.replace(/page\/\d+\//, '')
// => "http://jackson/search/?features=Sea%20View&submit=search"

Try something like this,
var a='http://jackson/search/page/3/?features=Sea%20View&submit=search'.split('/');
console.log(a.slice(0,4).join('/')+'/'+a.slice(6).join('/'));
Fiddle Demo

You can use this Javascript:
if (location.href.indexOf("/page/") > -1) {
location.assign(location.href.replace(/\/page\/\d+\//, "/"));
}

Related

Remove characters from url hash

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/.

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

How to find in javascript with regular expression string from url?

Good evening, How can I find in javascript with regular expression string from url address for example i have url: http://www.odsavacky.cz/blog/wpcproduct/mikronebulizer/ and I need only string between last slashes (/ /) http://something.cz/something/string/ in this example word that i need is mikronebulizer. Thank you very much for you help.
You could use a regex match with a group.
Use this:
/([\w\-]+)\/$/.exec("http://www.odsavacky.cz/blog/wpcproduct/mikronebulizer/")[1];
Here's a jsfiddle showing it in action
This part: ([\w\-]+)
Means at least 1 or more of the set of alphanumeric, underscore and hyphen and use it as the first match group.
Followed by a /
And then finally the: $
Which means the line should end with this
The .exec() returns an array where the first value is the full match (IE: "mikronebulizer/") and then each match group after that.
So .exec()[1] returns your value: mikronebulizer
Simply:
url.match(/([^\/]*)\/$/);
Should do it.
If you want to match (optionally) without a trailing slash, use:
url.match(/([^\/]*)\/?$/);
See it in action here: http://regex101.com/r/cL3qG3
If you have the url provided, then you can do it this way:
var url = 'http://www.odsavacky.cz/blog/wpcproduct/mikronebulizer/';
var urlsplit = url.split('/');
var urlEnd = urlsplit[urlsplit.length- (urlsplit[urlsplit.length-1] == '' ? 2 : 1)];
This will match either everything after the last slash, if there's any content there, and otherwise, it will match the part between the second-last and the last slash.
Something else to consider - yes a pure RegEx approach might be easier (heck, and faster), but I wanted to include this simply to point out window.location.pathName.
function getLast(){
// Strip trailing slash if present
var path = window.location.pathname.replace(/\/$?/, '');
return path.split('/').pop();
}
Alternatively you could get using split:
var pieces = "http://www.odsavacky.cz/blog/wpcproduct/mikronebulizer/".split("/");
var lastSegment = pieces[pieces.length - 2];
// lastSegment == mikronebulizer
var url = 'http://www.odsavacky.cz/blog/wpcproduct/mikronebulizer/';
if (url.slice(-1)=="/") {
url = url.substr(0,url.length-1);
}
var lastSegment = url.split('/').pop();
document.write(lastSegment+"<br>");

Strip with .exec Regular Expressions Javascript

I need to strip down a string which is a url.
Example:
"http://www.wearepi.com/wp-content/gallery/03-05-2011-asian-escape/img_2377.jpg"
I need to strip it down to:
"gallery/03-05-2011-asian-escape/img_2377.jpg"
i already got something like this:
/[^\/]*(?=\.\w+$)/.exec
But that just leaves me with:
"img_2377"
Thanks in advance for your help!
What about?
var target = "http://whatever.thing.com/this/that/theother.jfoo";
var matches = /^.*\/(gallery\/.*)$/.exec(target);
console.log( matches[0] );
That should have the right capture data in it.

javascript: if first 2 chars are //, replace it with /

I think I may need a regex for this, but since I suck at regexs was hoping someone here could spare a minute to help me.
Basically I have a variable (lets name it: zippy)
and if zippy's value is //blah.html
I want to delete one slash from there so it becomes /blah.html
(the 2 slashes will ALWAYS be in the first two characters, IF they exist at all)
How do I do this?
Thanks!
Regex would work, so would
zippy = (zippy.substr(0,2)=="//" ? zippy.substr(1) : zippy);
zippy = zippy.replace('//', '/');
Can't be simplier:
zippy=zippy.replace('^/{2}','/');
Also +1 for variable names.
if(zippy.substring(0,2) == '//')
{
zippy = '/' + zippy.substring(2);
}
I'm under the impression that substring(from,to) has from inclusive and to exclusive. But something to that effect. I don't know if javascript has a startsWith method.
Edit: Oh if the slashes will always be at the beginning than go with replace.
var zippy = "//blah.html"
var zippy_fixed = zippy.replace(/^\/\//, "/")
Another;
zippy = zippy.substr(1 + zippy.indexOf("//"));

Categories

Resources