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
Related
This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)
This question already has answers here:
How to replace plain URLs with links?
(25 answers)
Closed 2 years ago.
I just can't get my head around regex and this is driving me crazy.
I have the following string;
'This is a random string of text with a link to google.com/test and another link to facebook.com`
What I want to do, is turn google.com/test into https://google.com/test but leave the facebook.com link as plain text.
So basically, any instance of google.com (Including with prefixes) would turn into a link, but any other URL would remain as plain text.
Can someone give me a nudge in the right direction please?
Simple replace() will do:
var str = 'This is a random string of text with a link to google.com/test and another link to facebook.com';
str = str.replace('google.com/test', 'https://google.com/test');
console.log(str);
This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
Closed 5 years ago.
I'm developing a chrome and firefox extension and i'm stuck with matching a certain tag and content inside of that. Can you please help me out?
Code:
[QUOTE=UserAdmin;22061013]
[SIZE="4"]
[LEFT]
[COLOR="DarkGreen"] Sample text goes here [/COLOR]
[/LEFT]
[/SIZE]
[/QUOTE]
Here i'd like to match beginning of [QUOTE= because everything what comes after that will be totally different each time and finally by the closing tag of [/QUOTE]
I'm not a regex expert and here is what i've came up with:
const regex = /^(\[QUOTE=)/;
const str = "[QUOTE=UserAdmin;22061013][SIZE="4"] [LEFT] [COLOR="DarkGreen"] Sample text goes here [/COLOR] [/LEFT] [/SIZE][/QUOTE]";
It successfully matched as below but i'm not sure this is the correct way of doing it:
If i can have a regex code to match whatever inside the [QUOTE=]....[/QUOTE] tag and save it to later use would be highly appreciated.
Online regex fiddle link
Try this
\[QUOTE=[\s\S]+\[\/QUOTE\]
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)
This question already has answers here:
JavaScript regex multiline text between two tags
(5 answers)
Closed 4 years ago.
Even if I use the m flag, javascript regex seems to isolate regex matching by lines.
Example:
"if\nend".match(/if(.*?)end/m)
=> null
I want this to match. How do I get around this?
You actually want s (a.k.a. "dotall"), not m, but javascript doesn't support that. A workaround:
"if\nend".match(/if([\s\S]*?)end/)