zoukankan
html css js c++ java
将地址转换为链接的正则表达式(regex url href)
将文章内容中没有链接的地址转换为链接。
代码如下:
public
static
string
ShowUrls(
string
text)
{
//
代码来自博客园
http://www.cnblogs.com
Regex linkRegex
=
new
Regex(
"
href\\s*=\\s*(?:(?:\\\
"
(
?<
url
>
[
^
\\\
"
]*)\\\
"
)
|
(
?<
url
>
[
^
\\s]
*
))
"
,
RegexOptions.IgnoreCase
|
RegexOptions.Compiled);
MatchCollection linkMatchs
=
linkRegex.Matches(text);
string
pattern
=
@"
(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])
"
;
MatchCollection matchs;
string
clearText
=
Regex.Replace(text,
"
(<br>|<br/>)
"
,
"
"
, RegexOptions.IgnoreCase);
clearText
=
Regex.Replace(clearText,
"
<[^>]*>
"
,
string
.Empty, RegexOptions.Compiled);
//
清除html标记
matchs
=
Regex.Matches(clearText, pattern, RegexOptions.IgnoreCase
|
RegexOptions.Compiled);
bool
flag1
=
true
;
for
(
int
i
=
0
;i
<
matchs.Count;i
++
)
{
Match m
=
matchs[i];
string
link
=
"
<a href=\
""
+ m.ToString() +
"
\
"
target=\
"
_blank\
"
>
"
+
m.ToString()
+
"
</a>
"
;
if
(linkMatchs.Count
>
0
)
{
foreach
(Match linkMatch
in
linkMatchs)
{
if
(linkMatch.Value.IndexOf(m.Value)
>
-
1
)
{
flag1
=
false
;
break
;
}
}
}
if
(flag1)
{
bool
flag2
=
true
;
for
(
int
j
=
0
; j
<
i; j
++
)
{
if
(m.ToString()
==
matchs[j].ToString())
{
flag2
=
false
;
}
}
if
(flag2)
{
text
=
text.Replace(m.ToString(), link);
}
}
}
return
text;
}
查看全文
相关阅读:
js以字符串方式创建DOM(原生js,jquery,extjs)
gallery3
检测标准类型和内置对象类型
js数据类型和类型检测
gallery2
gallery
如何使用Git上传项目代码到github
sublime EMMET
模糊搜索
导出表格
原文地址:https://www.cnblogs.com/dudu/p/1006119.html
最新文章
设置网页背景图片不随网页内容放大缩小
IIS将错误信息发送到浏览器
申明版权专用
设置DIV可编辑
图片滚动
Android欢迎界面
android布局居中
Android开机启动程序
全透明Activity
标题栏Menu
热门文章
关于ActionBar
jquery插件开发示例(二)
(function(window,undefined)(window))
jquery插件开发示例
前端常用插件收藏
style.width,offsetWidth,clientWidth,scrollWidth的区别
style.left,offsetLeft,clientLeft,scrollLeft的区别
div块级元素内文字换行word-wrap, word-break,white-space。
js设计模式——桥接模式
原生js的Function,Array,Object构造函数的prototype原型方法扩展
Copyright © 2011-2022 走看看