Extract Video ID from youtube link in Javascript/Regex [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
jQuery Youtube URL Validation with regex
YouTube url id Regex
http://www.youtube.com/watch?v=SCS7SIeF30E
http://www.youtube.com/watch?v=SCS7SIeF30E&feature=relmfu
http://www.youtube.com/v/SCS7SIeF30E
How can I grab videoid SCS7SIeF30E from the above possible URL using javascript regular expression.
I know it has been asked many time but I am searching for Regex which cover
v/ also in url all regex only covers v= type url.

var myString = "http://www.youtube.com/v/SCS7SIeF30E";
var newString = myString.replace(/http:\/\/www.youtube.com\/v\//, "");
alert(newString);

Related

Javascript - Find twitter short url from tweet [duplicate]

This question already has answers here:
What is a good regular expression to match a URL? [duplicate]
(5 answers)
Closed 6 years ago.
I am looking for a jquery code to find the url part from a tweet and alert it. I have this tweet wrapped inside a div with a class .tweet
some tweet text goes here https://t.co/CTyPa0sYmp
I tried Regx expression but I guess because of the uppercase in url, it can only find the beginning of it.
var searchText = jQuery('.tweet').text(),
urls = searchText.match(/\b(https)?(:\/\/)?(\S*)\.(\w{2,4})\b/i);
alert(urls);
but the code only alerts "https://t.co"
What exactly am I doing wrong here?
JSFIDDLE
Copied the following regex from here, should work in your case. And I'm marking this question as duplicate.
/\b((http|https)?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,})\b/ig

Remove everything after domain and http in url javascript [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 7 years ago.
I have the full url in a javascript variable and I want to strip it down to the bare url.
For example:
https://www.google.com/hello/hi.php,
http://youtube.com
strip down to:
www.google.com, youtube.com
How would I do this in javascript?
Thanks
This is not similar to the other links as I am doing this within a chrome extension, therefore the only way to get the url is using the chrome extension api which only provides the full url. As such I need to strip the full url
You can try this:
\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)
Regex live here.
Live JavaScript sample:
var regex = /\/\/([^\/,\s]+\.[^\/,\s]+?)(?=\/|,|\s|$|\?|#)/g;
var input = "https://www.google.com/hello/hi.php, http://youtube.com,"
+ "http://test.net#jump or http://google.com?q=test";
while (match = regex.exec(input)) {
document.write(match[1] + "<br/>");
};
Hope it helps
Use the global window.location.hostname variable, and it will give you this information.

JavaScript regex for URL [duplicate]

This question already has answers here:
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 8 years ago.
I have to run a JavaScript regex on URLs to detect any (one or more) special character after URL and put space before them, like:
url, --> url , or url), --> url ),
e.g.
(https://www.microsoft.com), into (https://www.microsoft.com ),
var myregexp =/(\b(https?|ftp|file|http):\/\/[\-A-Za-z0-9+&##\/%?=~_|!:,.;]*[\-A-Za-z0-9+&##\/%=~_|])/g;
var str="http://www.microsoft.com)"; //url
var res=myregexp.exec(str); //execute regex query
str=str.substring(0,myregexp.lastIndex)+' '+str.substring(myregexp.lastIndex);
I used Regex query for url from : https://stackoverflow.com/a/163684

How to check string in current URL with javascript? [duplicate]

This question already has answers here:
Get current URL with jQuery?
(33 answers)
Closed 8 years ago.
My Url looks like:
http://mywebsite/series/2545-abc
But I don't know how to check URL with regex. So, How to check url current windows with above URL?
I mean:
If Url = http://mywebsite/series/2545-abc
do something
What about this? The regular expression defined at the first line is tested against the URL:
var myRe = /\/series\//; //regular expression to use
if(myRe.test(window.location.href)){
alert("matching");
}else{
alert("not matching");
}
If you want to test for the whole URL (and really want to use a regular expression for that), you could replace the first line with
var myRe = /^http:\/\/mywebsite\/series\/2545-abc$/;
If you remove the dollar sign at the end, modifications at the end of the URL are accepted as well (e.g. http://mywebsite/series/2545-abc/foo.html)

javascript url regexp restrict multiple http(s)/www [duplicate]

This question already has answers here:
Regular expression for URL
(10 answers)
Closed 8 years ago.
here is my regexp
var url_reg = /^(http[s]?:\/\/|ftp:\/\/)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|in|au)+/;
it works fine for single input like https://www.google.com , but
it allows double or more "http/https/www" like below -
https://www.google.com/https://www.google.com/
url can also include folder like google.com/folder/file
i need to validate single occurrence of valid url.
Can anyone help me?
To validate a URL, you can use a regex. This is what I use. A valid URL per the URL spec. The URL you have provided, is actually a valid URL per the URL spec.
/^((((https?|ftps?|gopher|telnet|nntp):\/\/)|(mailto:|news:))(%[0-9A-Fa-f]{2}|[-()_.!~*';\/?:#&=+$,A-Za-z0-9])+)([).!';/?:,][[:blank:]])?$/
This was borrowed from OSWAP

Categories

Resources