html textarea regex match [duplicate] - javascript

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

Related

Remove everything before the first forward slash in the url [duplicate]

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 need to remove everything before the third forward using regex so that for example https://stackoverflow.com/questions/ask becomes /questions/ask I'm not the greatest when it comes to regular expressions so your help would be much appreciated.
This is what I have so far https://regex101.com/r/qQ2dE4/498
The code I currently have is but want to use regex:
url.substring(url.indexOf('\/') + 3)
Use this:
(?<=.*\/.*\/.*\/).+
Demo
Explanation:
(?<= - its positive look behind, in any position that's pattern inside it is find matching start from this position to forward.
.*\/.*\/.*\/ - it is used inside the positive look behind, it cause matching start after the position that behind that position is 3 forward slashes
.+ - match one or more of from anything
Edit:
From #JaromandaX's comment, this can also be used (and honestly I think it more readable and simper):
(?<=(?:.*?\/){3}).+
I understand the questions asks for regex but the need is unclear - if all you need is the path name there are better tools in the toolbox (for anybody not working on a homework exercise).
const url = 'https://stackoverflow.com/questions/ask'
console.log(new URL(url).pathname)

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

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

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

Replace quote sign in JavaScipt [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I got following code snippet, it's pretty simple,
var b = "'aa','bb'";
console.log(b.replace("'", ""));
// result is "aa','bb'"
I want replace all single quote signs with blank. So my expected output should be "aa,bb", but the actual output is "aa','bb'" neither run this code snippet in Node nor browser. Seems only the first single quote sign has been replaced.
I already got a workaround to resolve this problem by replace with regex. What I wanna know
is what happened to replace function here? I cannot figure this out.
Try using RegEx specifying the global flag (g) that matches the pattern multiple times. Please also note that, as replace() does not modify the original string you have to reassign the modified value to the variable:
var b = "'aa','bb'";
b = b.replace(/'/g, "");
console.log(b);

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