一段字符中匹配出网址,可以将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 regex = "\\((.+?)\\)"; //匹配括号内内容 var arr = str.match(regex); // var reg = /http:\/\/\w*(\.\w*)+/ig;//匹配网址 console.log(arr[1]);
// var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/; 匹配邮箱 btn.onclick = function() { var text = document.getElementById('text'); var result = document.getElementById('result'); var str1 = text.value; var arr1 = str1.match(regex); console.log(arr1[1]); result.innerHTML = arr1[1]; result.href = arr1[1]; } } </script> <input type="text" name="text" id="text" placeholder="粘贴至此" value="" /> <button id="btn">链接</button> <a href="#" id="result"></a> </head> <body> </body> </html>
https://regexper.com/#%5C%5C((.%2B%3F)%5C%5C)