This question already has answers here:
How to check if a string "StartsWith" another string?
(18 answers)
Closed 7 years ago.
I would like to check the url that the user is saving and specially that it needs to be in a special format so i can use it at a later stage. The input field is to save a youtube video link and i would like to make sure that it is an actual youtube link or else present the user with an error stating it is not the proper format.
https://www.youtube.com/watch?v=y4Yp3-llWDs
I need to make sure that the beginning part of the url is proper always starting with this: https://www.youtube.com/watch
What would be the best way to go about this? regex?
var url = 'https://www.YouTube.com/watch'
if (/^https:\/\/www.youtube.com\/watch/i.test(url)) {
console.log('Valid YouTube URL Detected')
} else {
console.error('Invalid YouTube URL Detected')
}
Related
This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
I have a bug in my wysiwyg editor.
I have a box where I past a tweet url, and I must extract the ID to replace it with the embed code.
regex: {
twitter: /https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
}
if (str.match(this.opts.regex.twitter)) {
parsed = str.replace(this.opts.regex.twitter, '<blockquote class="twitter-tweet tw-align-center">TWEET $1</blockquote>');
return parsed;
}
The bug is in my regex
/https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
If I take this url
https://twitter.com/howard3141/status/1330546000969273347
It doesn't work because 'howard3141' has some numbers inside.
Since you need to fetch from a URL, window.location provides great apis to get value.
Following is the demonstration of it.
Note: I'm using URL to create a location like object, but it should work with window.location as well
var url = new URL('', 'https://twitter.com/howard3141/status/1330546000969273347')
console.log(url.pathname.match(/\/([^\/]*)\//)[1])
This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
My links are like that:
index.php
index.php?do=that
index.php?do=this
And my current javascript code is like that:
var url = window.location.pathname,
urlRegExp = new RegExp(url.replace(/\/$/,'') + "$");
console.log(url);
$('.nav-link').each(function(){
if(urlRegExp.test(this.href.replace(/\/$/,''))){
$(this).addClass('active');
}
});
});
So with this code even if I'm at index.php?do=this console log says to me /path/index.php
I think I need make changes on var url = window.location.pathname line but I couldn't.
How should I improve my code? There are bunch of similiar questions here in SO, but not the same one.
I appreciate any help.
In PHP you can get your URL parameters from GET array.
$_GET['do'] or try to print $_GET. This array will print all the URL parameters.
Hope this helps!
This question already has answers here:
Get the current URL with JavaScript?
(28 answers)
Closed 8 years ago.
I have this js function
function twitterShare(){
window.open( 'http://twitter.com/intent/tweet?text='+$(".section-title h1").text() +'
'+window.location, "twitterWindow",
"height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0")
return false;
}
When the twitter window opens it grabs the .section-title h1 class, but I need it to say something like this,
"Check this out here: http://currentwebsiteurl.com"
I need a text string then grab the url of the page.
Any help would be greatly appreciated.
window.location.href contains the current URL of the page.
So your function should look like this:
function twitterShare(){
window.open('http://twitter.com/intent/tweet?text=Check this out here: ' + window.location.href,
"twitterWindow", "height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0")
return false;
}
You can use window.location.href. It holds the value of the current page you are on.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to rewrite URL without refresh, like GitHub.com
let say I have variable in Javascript
var currentpage = 0;
my address page address is www.xyz.com/page/1
when I click somewhere and js function trigger
currentpage = 1
//Here I want to change/rewrite www.xyz.com/page/2
//Without redirecting to this page
See pushState (browser support currently limited).
You can't. You have to use the '#' or '#!' notation and pass the page number after it, then do trigger some js on load to figure out which page to display.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
how can i validate a url in javascript using regular expression
I want to validate whether user input a valid internet adress like
www.hotmail.com
http://www.abc.com.pk
Here's the js function:
function validateString(regexPattern, testString) {
var regexObj = new RegExp(regexPattern);
return regexObj.test(testString);
}
Finding the regex for URLs is just a matter of typing it in google. Here's a good one I've used before.