检测并从文本区域中删除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, '');