检测并从文本区域中删除URL [英] Detect and remove URLs from textarea

查看:85
本文介绍了检测并从文本区域中删除URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<textarea name="test">
  http://google.com/
  https://google.com/
  www.google.com/
  [url=http://google.com/]google.com[/url]
  text
</textarea>

我当前的尝试是检查文本区域中是否存在URL.

My current attempt at checking if there is a URL in the textarea.

if ($('textarea[name="test"]').val().indexOf('[url') >= 0 ||
    $('textarea[name="test"]').val().match(/^http([s]?):\/\/.*/) ||
    $('textarea[name="test"]').val().match(/^www.[0-9a-zA-Z',-]./)) {

对于检查上述任何URL,这似乎并不完全有效-我想知道如何对其进行优化.此刻似乎很草率,被黑了,希望有人能提供一些见识.

This doesn't seem to work completely for checking any of the URLs above - I'm wondering how it can be optimized. It seems very sloppy and hacked together at the moment and hopefully someone can shed some insight.

我目前从文本区域中删除URL的尝试:

My current attempt at removing URLs from the textarea:

var value = $('textarea[name="test"]').val();
    value = value.replace(/\[\/?url([^\]]+)?\]/g, '');
$('textarea[name="test"]').val(value);

现在,它将输出:

<textarea>
  http://google.com/
  https://google.com/
  www.google.com/
  google.com
  text
</textarea>

我想要的输出是

<textarea>
  text
</textarea>

推荐答案

尝试(评论后更正和改进):

Try (Corrected and improved after comments):

value = value.replace(/^(\[url=)?(https?:\/\/)?(www\.|\S+?\.)(\S+?\.)?\S+$\s*/mg, '');

从头到尾剥离表达式:

  • 除了方案外,地址可能包含两个或三个部分"
  • 地址可能以 www 开头
  • http:// https://
  • 之前
  • 它可能包含在 [url = ...] ... [/url]
  • An address might have two or three 'parts', besides the scheme
  • An address might start with www or not
  • It my be preceeded by http:// or https://
  • It may be enclosed inside [url=...]...[/url]

此表达式未强制使用完整的正确语法,这是编写起来要困难得多的正则表达式.
您可能需要一些改进:

This expression does not enforce the full correct syntax, that is a much tougher regex to write.
A few improvements you might want:

1.空间意识

value = value.replace(/^\s*(\[\s*url\s*=\s*)?(https?:\/\/)?(www\.|\S+?\.)(\S+?\.)?\S+\s*$\s*/mg, '');

2.在最后部分不加点

2.Enforce no dots on the last part

value = value.replace(/^(\[url=)?(https?:\/\/)?(www\.|\S+?\.)(\S+?\.)?[^.\s]+$\s*/mg, '');

这篇关于检测并从文本区域中删除URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆