判断url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
NSString*judgeString [email protected]"^(http|https)://([\w-]+.)+[\w-]+(/[\w-./?%&=]*)?$";
NSPredicate * preddicate =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",judgeString];
BOOL isValiu =[preddicate evaluateWithObject:urlString];
if (isValiu) {
NSString * judgeString = @"http://|https://";
NSPredicate * preddicate =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",urlString];
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:judgeString options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *result = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
NSString *imageBeforeText = nil;
NSString *imageName = nil;
while (result) {
//http//
imageName = [string substringWithRange:result.range];
//获取http之前的文字
imageBeforeText = [string substringToIndex:result.range.location];
//获取url
string = [string substringFromIndex:result.range.location + result.range.length];
result = [regex firstMatchInString:string options:0 range:NSMakeRange(0, [string length])];
}
NSString * isUrlString = [NSString stringWithFormat:@"%@%@",imageName,string];