Hi i have the following text in a div:
[http://www.google.com Google this link] some other random text [http://example.com and another website] but don't parse [this is not a link, leave me alone].
What I tried to do was to convert the links into normal html links. The Format is always like this, so it opens with a [ and then the url, followed by the link text and then a closing ]. But I only want to match links, not all text in square brackets.
I want to use the .match() function in javascript to do this task, but I wasn't able to figure out the regex expression (I only need the text parts that are links - the rest should be a simple split).
Any help would be apprechiated.
Why .match and not .replace?
string.replace(/\[(https?:\/\/[^\]\s]+)(?: ([^\]]*))?\]/g, "<a href='$1'>$2</a>");
Related
I have an HTML code that contain CSS code inside tag under the header tag. I want to use regex to extract all text in HTML, only pure text (between HTML tags ). I tried,
console.log(HTML_TEXT.replace(/(<([^>]+)>)/g, ""))
which replace every thing between <> by empty char, the problem is the CSS code inside STYLE tag is still there, so i want to know how to write the regular expression to remove CSS code inside tags.
How do I solve this problem?
This RegEx might help you to do so:
(\>)(.+)(<\/style>)
It creates a right boundary in a capturing group: (<\/style>)
It has a left boundary in another capturing group: (\>), which you can add additional boundaries to it, if you wish/necessary
Then, it has a no-boundary middle capturing group, (.+), where your target is located, and you can call it using $2 and replace it with an empty string, or otherwise.
I'm not so sure, did not test it, but your code might look like something similar to:
console.log(HTML_TEXT.replace(/(\>)(.+)(<\/style>)/g, '\\$1\\$3'))
This post explains how to do a string replace in JavaScript.
Edit:
Based on the comment, this RegEx might help you to filter your tags using $1:
(\<style type=\"text\/css\"\>)([\s\S]*)(\<\/style\>)
I have a regex that first prettifies any text between back-ticks (``) as code. And then I use another regex that detects urls and generates anchor tag. Just like stackoverflow's editor. The problem is I don't want to urlify the links in code-prettified part.
For example the first url in the following example should be urlified but the second one shouldn't:
To generate a link to http://stackoverflow.com you should write
StackOverflow
I want to do an online chatting application, its sending emoji ability like this:
So I use an div whose contentEditable is true as input area. And all of the message would be saved in database in this way:
"Hello, Cathy<img src="/public/face/Mazes_Mini_017.png"><img src="/public/face/Mazes_Mini_017.png">"
All of these above works well, but at that time I want to do a search ability, highlighting the key word I search, I met a problem when I want to use string.replace method to replace myKeyWord with <span class='highlight'>myKeyWord<span>, but it would match some characters in <img> tag...
eg. <img src="/public/face/Mazes_Mini_017.png">112233test112233</img>123test123
keyword is test
expected result is:
<img src="test.png">112233test112233</img>123test123
So I really want to use regExp to matched some characters not in a specify tag like <img>, I have tried a lot but no one worked...
Here is an example
You could try this: (?<!<\/)test(?![^<]*<\/img>)
The function of the jquery code I currently have is to detect text enclosed with parenthesis and it will serve as a text anchor to a link.
The problem is that parenthesis appears on the text anchor too. Is it possible to show only the text? Also if it's possible to change the parenthesis() into brackets []. thanks in advance.
$("body").html($("#wrapper").html().replace(/(\([^)]+\))/, "<a href='https://www.sample-url-here.com/'>$1</a>"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="wrapper">text (link here)</div>
The outcome I need should be:
from text [link here] to text link here
What about this? (jsfiddle)
$("body").html($("#wrapper").html().replace(/\(([^)]+)\)/, "[<a href='https://www.sample-url-here.com/'>$1</a>]"));
This will output a link surrounded by square brackets, with only the link underlined.
The problem with you original regular expression was that it was looking for anything not parentheses, surrounded by anything that is parenthesis, and that whole expression was surrounded by capturing parentheses.
In other words, let's look at it like this:
(x)
We'll call those your capturing parentheses. x can later be selected with $1.
x = \([^)]+\)
See the problem? Your entire x was being captured.
EDIT
jsfiddle updated to not show brackets.
EDIT
How about this one? jsfiddle
Currently I have this regex to split a paragraph into sentences: /[^\.!\?]+[\.!\?]+/g. The issue though is that my paragraphs aren't just paragraphs of text. I have links in them like this:
This is text and here is a <value="link" href="http://link.com?param=test"> which directs to another page. So I don't want to split at the anything inside the link above.
I want to split that into an array like:
['This is text and here is a <value="link" href="http://link.com?param=test"> which directs to another page.', 'So I don't want to split at the anything inside the link above.']
What regex would do this?
Try this:
(.+?[\.!\?](?!.+?>)\s*)