Regex - Check if string contains partial URL and convert to link [duplicate] - javascript

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

Related

Replace multiple strings in a text file with one string using javascript [duplicate]

This question already has answers here:
Replace a line in txt file using JavaScript
(2 answers)
Closed 1 year ago.
I'm trying to replace multiple strings in a text file with one string using javascript
I want those two strings "user" and "password" must be replaced with 'replace the string'
Anyone help me here?
to add to the previous replies, you can use capture groups to back reference parts of the matched text i.e.
if you search for "user"
let newText = yourText.replace(/^((\"user\")(:".*")(.*))$/gm, '"removed"$3$4' );
if you search for "1232"
let newText = yourText.replace(/^((\".*\")(:"1232")(.*))$/gm, '"removed"$3$4' );

how to get the whole string matched in regex [duplicate]

This question already has answers here:
How do I find words starting with a specific letter?
(2 answers)
Closed 2 years ago.
I have this code
const paragraph = 'my name is bright and this is a testing interface, right.';
const regex = /\b(b)/g;
const found = paragraph.match(regex);
console.log(found);
What i want is that i want to get the whole word instead of just a single letter.
e.g the output in this code above is b which is gotten from the string bright in the paragraph but i don't just want the b but the word bright as a whole and still be able to manipulate it like make it bolder or something else. Please how do i do it and i have also checked other similar questions on stackoverflow but nothing
Assuming you only want to match words delimited by spaces, this should do the trick.
((?:\w)+)

Split text by urls [duplicate]

This question already has answers here:
Javascript and regex: split string and keep the separator
(11 answers)
What is the best regular expression to check if a string is a valid URL?
(62 answers)
Closed 3 years ago.
I need to split a given text by urls that it might contain, while keeping the urls-separators in the resulting array.
For example splitting this text:
"An example text that contains many links such us
http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and
link-4.com."
would result into this array:
["An example text that contains many links such us ", "http://www.link1.com", ", ", "https://www.link2.com/path?param=value", ", ", "www.link3.com", " and ", "link-4.com", "."]
I tried to use String.protoype.split() with a regular expression, but it's not working as it contains unwanted parts of the urls themselves:
var text = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
console.log(text.split(/((https?:\/\/)|([\w-]{2,}[.])+([\S]{2,})[^\s|,!$\^\*;:{}`()])+/ig));
EDIT
This question is different than the suggested ones, my purpose is not to check if a url is valid or not, but to find a regular expression susceptible to be used in the split method, and that splits correctly the text.
As for splitting a text by regex, it is already used in the snippet sample. What is proposed in the suggested question is more general, and what I am looking for is more specific to urls.
it's not ideal and it would be hard to find or create perfect regex for it that you going to test all cases but you can quickly write something like this:
var text2 = "An example text that contains many links such us http://www.link1.com, https://www.link2.com/path?param=value, www.link3.com and link-4.com.";
text2
.split(/(^|\s)((https?:\/\/)?[\w-]+(\.[\w-]+)+\.?(:\d+)?(\/\S*)?)/ig)
.filter(Boolean)
.filter((x)=>{ return x.indexOf('.')>0 })

html textarea regex match [duplicate]

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

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

Categories

Resources