Fix a regex that extract twitter id from a twitter url [duplicate] - javascript

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])

Related

How to get url parameter and add active class witt javascript/jquery? [duplicate]

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!

Check for url validation format [duplicate]

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')
}

How to filter elements with regex expression [duplicate]

This question already has answers here:
Test if links are external with jQuery / javascript?
(16 answers)
Closed 8 years ago.
I want to select all the a elements which point to external links, so I want a selector which picks only the href values matched with
/https?:\/\/[^\/\!\?\<\>\_\*\&\(\)\#\$\#]*/
that are different from
window.location.hostname
what's the correct expression?
This would be more robust solution, checking for different location hostname than main window:
var $externalLinks = $('a[href]').filter(function(){
return this.hostname != window.location.hostname;
});
-jsFiddle-
$("a:not([href*="+window.location.hostname+"])[href*=http], a:not([href*="+window.location.hostname+"])[href*=https]")
Will give you all the links not having window.location.hostname but having http or https
No regex needed.
Try this (using Jquery):
var links = $('a[href^="http"]');

Create links out of all URLs in an arbitrary string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to replace plain URLs with links?
I am looking to make the user's life a little easier by parsing text input (via javascript) to find all URLs, then link them (wrap tags) so users can just click them. Facebook and even SO does something similar to give you an idea of what I'm looking for.
function censorLinks(text) {
return text.replace(
new RegExp('(https?://[^\\s]+)', 'g'),
'$1'
);
}
I don't need to support every protocol. I think http and https is find for now. I'm assuming everything beyond http:// up to the white space is a URL. This almost works but there is one tiny problem: if the URL ends with a period, the period gets included. I suppose it is a valid URL but I think it is unlikely in my situation. How can I prevent the period?
Like #Truth said, this may be a duplicate post but...
I was recently doing something similar, haven't tested this but maybe this'll wrok...
var email = /(\w+#[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;
var url = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/ig;
function convert(input) {
return input
.replace(url,'$1')
.replace(email, '$1');
}
Edit: fixed typo

how to get parameter value from a url using js [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get query string values in JavaScript
the link is
http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27
how can I parse the parameter "Text"(at the end) from the url using javascript?
anyone can help me?
URI.js is a small library that makes working with URLs nice and easy :)
var url = new URI("http://nycs00058260/sites/usitp/_layouts/CreateWebPage_DingDing.aspx?List={74AB081E-59FB-45A5-876D-284607DA03C6}&RootFolder=%3b&Text=%27MDNSO%27");
var queryMap = URI.parseQuery(url.query());
var text = queryMap["Text"];
console.log("Text: ", text);
You can combine window.location.search : https://developer.mozilla.org/en-US/docs/DOM/window.location
with indexOf : https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf
and substring: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/substring

Categories