正则表达式模式不返回匹配

时间:2016-12-21 06:06:37

标签: javascript asp.net regex vb.net

我有以下正则表达式代码来检查字符串中的所有网址并添加超链接

<body>
<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var str = "google.com is here http://www.google.com"
    //  str += "af sfda sdajsd a"
     //   str += " google.com"



var patt1 =  /(\b(?:(https?|ftp):\/\/)?((?:www\d{0,3}\.)?([a-z0-9.-]+\.(?:[a-z]{2,4}|museum|travel)(?:\/[^\/\s]+)*))\b)/g;

    var result = str.replace(patt1, function(url) {
        return '<a href="' + url + '">' + url + '</a>';
    });

    alert(result)
    document.getElementById("demo").innerHTML = result;
}
</script>

</body>
</html>

这工作正常,但是当我在服务器端使用相同的模式时,它不会返回任何匹配。那是什么,我做错了

我的vb.net代码如下

Dim regex As New Regex("/(\b(?:(https?|ftp):\/\/)?((?:www\d{0,3}\.)?([a-z0-9.-]+\.(?:[a-z]{2,4}|museum|travel)(?:\/[^\/\s]+)*))\b)/g;")

 Dim mactches As MatchCollection = regex.Matches(strtemp)

            For Each match As Match In mactches
                strtemp = strtemp.Replace(match.Value, "<a target='_blank' href='" & match.Value & "'>" & match.Value & "</a>")
            Next

1 个答案:

答案 0 :(得分:0)

我刚从模式中删除了/和/ g,它运行正常。