I have a url which looks like this
https://test.high.com/people/11111111-name-firstname-_custa/deals/new
Now i need to match document.URL
if im on that Page if so i will alert a message.
The important part is /deals/new
How can i match that in Javascript?
var regex = new RegExp("/deals/new$");
if(document.URL.match(regex))
alert("yeah");
A Regex matching any string ending with "/deals/new" is
"/deals/new$"
If you need your link to only contain /deals/new, try
"/deals/new(/|$)"
Do you mean that you want access to the documents url from javascript?
That is done via the documents location property.
Try document.location.href or only location.href.
Related
I have a URL as Follow:
http://example.com/category/news/page/2/
I need to replace any number that comes at the end of URL which represents page number.
If possible which I think it is, I want to use regular expression in case the domain changes, the code still works.
I am also using PHP ...
Could help me with a proper RegEx?
Find The Answer:
string.replace(/\/page\/[0-9]+/, '/page/' + pageNum);
pageNum can be any variable to replace the page number
There are lots of posts online like this, but none of them seem to do what I'm trying to do.
Let's say I have a domain in a string:
Extract hostname name from string
And I want to extract the domain name and nothing else (not the protocol, the subdomain or the file extension).
so for
https://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string
I want to get:
stackoverflow.com
Is there any way to do this?
Try this on:
var url = 'http://stackoverflow.com/questions/8498592/extract-root-domain-name-from-string';
var domain = url.match(/^https?:\/\/([^\/?#]+)/)[1];
alert(domain);
This looks for a string that starts with http and optionally s, followed by ://, then matches everything it can that is not a /. But .match() returns an array here:
['http://stackoverflow.com', 'stackoverflow.com']
So, we use [1] to get the submatch.
You can use a simple regex like this:
\/\/(.*?)\/
Here you have a working example:
http://regex101.com/r/iP0uX7/1
Hope to help
I have a string that may contain several url links (http or https). I need a script that would remove all those URLs from the string completely and return that same string without them.
I tried so far:
var url = "and I said http://fdsadfs.com/dasfsdadf/afsdasf.html";
var protomatch = /(https?|ftp):\/\//; // NB: not '.*'
var b = url.replace(protomatch, '');
console.log(b);
but this only removes the http part and keeps the link.
How to write the right regex that it would remove everything that follows http and also detect several links in the string?
Thank you so much!
You can use this regex:
var b = url.replace(/(?:https?|ftp):\/\/[\n\S]+/g, '');
//=> and I said
This regex matches and removes any URL that starts with http:// or https:// or ftp:// and matches up to next space character OR end of input. [\n\S]+ will match across multi lines as well.
Did you search for a url parser regex? This question has a few comprehensive answers Getting parts of a URL (Regex)
That said, if you want something much simpler (and maybe not as perfect), you should remember to capture the entire url string and not just the protocol.
Something like
/(https?|ftp):\/\/[\.[a-zA-Z0-9\/\-]+/
should work better. Notice that the added half parses the rest of the URL after the protocol.
I am trying to write a regex which will do the following
If I have url like
example.com?mask=33&filter=23423&mode=12
It will match substring filter=23423
I need also to consider that the url can be
example.com?filter=23423
Thanks in advance.
You may just not consider [\?&] in your regexp and put /filter=[0-9]*/ if you sure that there will not be any params like other_filter=987 in your url.
If you may have such use substr on the resulted regexp like so
url.match(/[\?&]filter=[0-9]*/)[0].substr(1)
I have limited JS experience, and I need to split a string that would look something like:
http://example.com/#!/about
Into http://example.com/ and !/about
I can't unfortuently use PHP and parsing the URL won't work. Right??
This is what I have at the moment:
<script type="text/JavaScript">
var newUrl = window.location.pathname;
var hash=newUrl.split('#');
var f=hash[1];
</script>
I could do this for the 3rd line:
var hash=newUrl.split('com');
And then account for the hash, but the problem with that is if someone goes to
example.com/index.php/#!/about
So I'd then have to double my code after this point.
Any ideas how to split the URL into two parts centring around the hash without having to use what I just mentioned??
The .pathname doesn't include the hash. Use the .hash property instead. Use substring to strip the # character from the front.
var hash = location.hash.substring(1);
If it's not your window.location, you can use /([^:]+://[^/]+)[^#]+#(.*)/ - you will get the required parts in the first and second capturing groups.