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, "")
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'm not using REGEX very often so I don't know it well.
Want to match last digits before / end of string.
so my regex will be\d+/$
Now I want to replace matched part of href inside the link.
First thing
SyntaxError: illegal character
var regex = \d+/$
so I escaped it (I think) var regex = /\d+//$
I thought it will be simple from now:
$('a').attr('href').replace(regex,'00/')
But it seems no use.
I'm using firebug console for testing
Solution
url = "www.example.com/event/detail/46/"
var value = url.substring(url.lastIndexOf('/') + 1);
url = url.replace(value, '00')
What you seem to want is this :
$('a').attr('href', function(_,h){ return h.replace(/\d+\/$/,'00/') });
A slash is escaped as \/ in a regex literal, not as //.
$(selector).attr(name, fun) will apply the function to each element.
In escaping use \ not /.
So this will be
var regex = /\d+\$/
I would like to match a path in a Url, but ignoring the querystring.
The regex should include an optional trailing slash before the querystring.
Example urls that should give a valid match:
/path/?a=123&b=123
/path?a=123&b=123
So the string '/path' should match either of the above urls.
I have tried the following regex: (/path[^?]+).*
But this will only match urls like the first example above: /path/?a=123&b=123
Any idea how i would go about getting it to match the second example without the trailing slash as well?
Regex is a requirement.
No need for regexp:
url.split("?")[0];
If you really need it, then try this:
\/path\?*.*
EDIT Actually the most precise regexp should be:
^(\/path)(\/?\?{0}|\/?\?{1}.*)$
because you want to match either /path or /path/ or /path?something or /path/?something and nothing else. Note that ? means "at most one" while \? means a question mark.
BTW: What kind of routing library does not handle query strings?? I suggest using something else.
http://jsfiddle.net/bJcX3/
var re = /(\/?[^?]*?)\?.*/;
var p1 = "/path/to/something/?a=123&b=123";
var p2 = "/path/to/something/else?a=123&b=123";
var p1_matches = p1.match(re);
var p2_matches = p2.match(re);
document.write(p1_matches[1] + "<br>");
document.write(p2_matches[1] + "<br>");
I found many solutions, but none was useful for me.
Let's say, as an example, I want to find URLs that start with www. and end with a space or ?. In this case, I really mean it ends in a ?, not that it's necessarily a CGI-related URL.
I'm trying to use the regex
var r = /(^|[\s\?])(www\..+?(?=([\s]|\?|($))))/g;
My sample use: http://jsfiddle.net/DKNat/2/
How can I use \? in a regex to prevent the end of the URL containing / before ??
http://jsfiddle.net/DKNat/11/
I can't solve last prob with DOT at the end of url.
Can any body help?
Try this in your fiddle:
var r = /(^|\??)(www\.[^\?]+)/g;
I updated your fiddle here:
http://jsfiddle.net/DKNat/3/
Update:
I see what you are trying to do now. Unfortunately, both your strings are essentially the same, apart from the /, so unless you want your regex to make the assumption that a ? anywhere after a slash denotes a CGI call, then there isn't much you can do. But you could try this:
var r = /(^|\??)(www\.[^\?]+\/[^\/]+\?[^\?]+|www\.[^\?]+)/g;
Updated fiddle:
http://jsfiddle.net/DKNat/5/
Update 2: After determining the requirements, this is the final RegExp I added to fiddle 10:
var r = /(^|[\?\s])(www\.[^\? ]+\/[^\/ ]*\?[^\? ]+|www\.[^\? ]+)/g;
Say I have a URL for an article:
http://domain.com/blog/articles/title-here/
And it has about 5 pages, so as you go through each page, you get:
http://domain.com/blog/articles/title-here/ OR http://domain.com/blog/articles/title-here/1
http://domain.com/blog/articles/title-here/2
http://domain.com/blog/articles/title-here/3
http://domain.com/blog/articles/title-here/4
http://domain.com/blog/articles/title-here/5
I know that the following code will get the full current URL (aka including the page #):
var u = window.location.href;
But is there a way to limit it so that the page # is NOT a part of the variable "u"?
Perhaps there's a regex or something I should add in there..? (I'm fairly new to javascript, so not sure how to apply this?)
var u = window.location.href.match(/.*[/][^\d]*/)[0]
Would that work for you?
Edit
I changed it... again :P
NOTE: Regex is a more complicated version of Joseph's and still suffers from the same bug. Will undelete when I fix it.
Joseph's answer is good, but it has a minor bug: it will drop the last part of the URL if you have an URL like:
http://domain.com/blog/articles/title-here
You can use this instead:
var u = window.location.href.match(/(.*)(\/\d*)/)[1]
How the regex works:
/ # delimiter
(.*?) # match anything and put in capture group 1
(\/ # match the forward slash
\d*) # match zero or more digits
/ # delimiter
var l = window.location;
l.href.replace(l.pathname, l.pathname.split(/\/[0-9]+$/)[0]);
try it in the console at this URL
Regex would do it. But in this case, you could just turn it into an array, strip of the end item, and re-serialize it.
var a = 'http://domain.com/blog/articles/title-here/2'.split('/');
a.splice(-1, 1);
a.join('/');