首页 > 代码库 > 正则表达式匹配网址

正则表达式匹配网址

一段字符中匹配出网址,可以将css里复制的url(blabla)匹配网址方便下图

<!DOCTYPE html>
<html>

    <head>
        <meta charset="UTF-8">
        <title>match</title>
        <script type="text/javascript">
            window.onload = function() {
                var str = "url(http://img2.cache.netease.com/f2e/wap/touch_index_2016/trunk/images/download_2017.png) no-repeat";
                var reg = /http:\/\/\w*(\.\w*)+/ig;
                var arr = str.match(reg);
                console.log(arr);
                
                
                
                
                btn.onclick = function() {
                    var text = document.getElementById(‘text‘);
                    var input = document.createElement(‘input‘);
                    var str1=text.value;
                    var arr1=str1.match(reg);
                    console.log(arr1);
                    input.value = arr1;
                    btn.appendChild(input);
                }

            }
        </script>
        <input type="text" name="text" id="text" placeholder="粘贴至此" value="" />
        <button id="btn">链接</button>
    </head>

    <body>
    </body>

</html>

 

正则表达式匹配网址