需求:有以下字符串javascript
"<p style='text-align: center;'><img title='2' border=0 style='max-width:600px;' zcmsimagerela='207904' src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_ed3d845c1dc5403084f6ac28eac55c2e.png' alt='2' /></p>;<p style='text-align: center;'><a alt='點擊查看大圖' href='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/02c0cc21cd294e8688a9241432429871.png' target='_blank' ><img title='1' border=0 style='max-width:600px;' zcmsimagerela='207903' src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_02c0cc21cd294e8688a9241432429871.png' alt='1' /></a></p>"
匹配出該字符串中src=後面的全路徑。
測試以下html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <script type="text/javascript"> var tmp = "<p style='text-align: center;'><img title='2' border=0 style='max-width:600px;' zcmsimagerela='207904' src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_ed3d845c1dc5403084f6ac28eac55c2e.png' alt='2' /></p>;<p style='text-align: center;'><a alt='點擊查看大圖' href='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/02c0cc21cd294e8688a9241432429871.png' target='_blank' ><img title='1' border=0 style='max-width:600px;' zcmsimagerela='207903' src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_02c0cc21cd294e8688a9241432429871.png' alt='1' /></a></p>"; <!--匹配出src後面全路徑--> var objectArr0 = tmp.match(/src=\'[^\']+/g); <!--匹配出全部的http請求--> var objectArr1 = tmp.match(/http:[^\']+/g); <!--匹配全部的IP地址--> var objectArr2 = tmp.match(/http:\/\/[^\/]+/g); <!--匹配出zcmsimagerela後面的數字-> var objectArr3 = tmp.match(/zcmsimagerela=\'[^\']+/g); <!--匹配出全部6位連續純數字--> var objectArr4 = tmp.match(/\d{6}/g); </script> <body> </body> </html>
得出結果以下
objectArr0值java
["src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_ed3d845c1dc5403084f6ac28eac55c2e.png", "src='http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_02c0cc21cd294e8688a9241432429871.png"]
objectArr1值測試
["http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_ed3d845c1dc5403084f6ac28eac55c2e.png", "http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/02c0cc21cd294e8688a9241432429871.png", "http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_02c0cc21cd294e8688a9241432429871.png"]
objectArr2值3d
["http://192.201.68.182:81", "http://192.201.68.182:81", "http://192.201.68.182:81"]
objectArr3值code
["zcmsimagerela='207904", "zcmsimagerela='207903"]
objectArr4值htm
["207904", "540308", "924143", "242987", "207903", "924143", "242987"]
其實完整的需求應該是
匹配出src=後面的http地址,獲得的結果應該是ip
["http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_ed3d845c1dc5403084f6ac28eac55c2e.png", "http://192.201.68.182:81/hxyxw//upload/Image/mrtp/2016/11/08/1_02c0cc21cd294e8688a9241432429871.png"]
我一次性慮不出來,只能濾出帶上src=的字符串。
望見此貼者提出寶貴的意見。字符串