I have a contentEditable DIV where I would like to replace any user typed URL (by matching the string with regular expression) with blue color text wrapped inside a span tag.
However, different browsers return different results. Besides, replacing the matched text with span puts cursor at the beginning of the text.
Here is the link: http://jsfiddle.net/Qz5ww/1
**CSS**
.class{
position:relative;
outline:none;
border:5px solid #96C;
font-size:16px;
width:500px;
height:60px;
padding:0px 2px;
word-wrap:break-word;
}
**HTML**
<div class='class' id='div' contentEditable='true'></div>
**JavaScript**
var regExUrl = /https?:\\/\\/([\\w\\d-\\.]+)?[\\w\\d-\\.]+\\.{1}[\\w]{1,4}(\\/{1})?([a-zA-Z0-9&-@_\\+.*​~#?\\/=]*)?/gi;
var div = document.getElementById('div');
div.onkeyup = function () {
if (div.innerHTML.match(regExUrl)) {
st = div.innerHTML.match(regExUrl);
div.innerHTML = div.innerHTML.replace(regExUrl, "<span style='color:blue;text-decoration:underline'>" + st[0] + "</span>");
}
}
How can I set the cursor at the end of the replaced text and continue typing with the default color (black)?
I would consider simplifying your regex to match anything beginning with http and containing a dot followed by one to five characters, or using something more powerful, such as this or [URL=“https://github.com/websanova/js-url”]this.
With that script you would have to break up the user input into groups of characters separated by spaces (as AFAIK a url can’t contain a space), then run each chunk through the function.
Depending on what kind of urls you are looking for, you would then check to see if (for example) myURL.protocol contained a value and myURL.host; contained a value with at least one dot.
Thanks for the reply, Pullo.
It isn’t related with this question, but nevertheless, if I submit data via AJAX from a contenteditable div to a php file, how should I treat the data before inserting it into the database?
I mean, how should I use htmlspecialchars()?
If I use them when sending response text, the content is displayed with tags (htmlspecialchars does what it is supposed to do), but I don’t want those tags displayed, I just want to prevent any XSS attack.
What I would do is use the PDO library and prepared statements to insert data into the DB. This will help protect against SQL injection attacks.
The data you get from the DB should be sanitized before being output to prevent XSS. DO this either by using htmlspecialchars()/samp], htmlentites()orstrip_tags()`, depending what you want to escape or delete.