Detecting URL in Textarea with Regex

0

Expensive, I would like to use the following example ( link ) that marks any link entered, and put in a textarea of my site to mark and warn that no links allowed. I tried with required pattern and could not. I also did the following example but it did not work in textarea:

function isUrl(s) {
    var regexp = (?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s'!()\[\]{};:'".,<>?«»“”‘’]))?
    return regexp.test(s);
}

I've done several other examples but so far I have not been able to. Someone could give me a little help to get me started. Before anyone else negative me, I did many examples, I've been researching for several days, and since I can not put all my tests here, I went straight to the first question and was denied. Thanks

    
asked by anonymous 14.08.2018 / 14:43

1 answer

1

See if I get it right .... execute it there.

// Add your javascript here
$(function(){
  
  function changeColor(parent) {
    var text = parent.text().replace(/\r/g, '');
    var newHTML = "";

    if(/(?:(?:https?|ftp):\/\/|\b(?:[a-z\d]+\.))(?:(?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))?\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))?\)|[^\s'!()\[\]{};:'".,<>?«»“”‘’]))?/.test(text)) {
      newHTML = "<span class='statement'>" + text + "\r</span>";
    } else {
      newHTML = "<span class='other'>" + text + "\r</span>";
    }
    parent.html(newHTML);

    var child = parent.children();
    var range = document.createRange();
    var sel = window.getSelection();
    range.setStart(child[child.length-1], 1);
    range.collapse(true);
    sel.removeAllRanges();
    sel.addRange(range);
    
    parent[0].focus();
  }
  
  changeColor($("#editor"));
  
  $("#editor").on("keydown keyup", function(e){
          changeColor($(this));
  });
});
#editor {
    width: 400px;
    height: 100px;
    padding: 10px;
    background-color: #444;
    color: white;
    font-size: 14px;
    font-family: monospace;
}
  
.statement {
    color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="editor" contenteditable="true">
</div>
    
14.08.2018 / 19:57