我的问题是如果加载了我的UIWebView,我该如何获得电子邮件地址......
首先我创建了一个关于View的字符串,我可以在我的字符串中找到@:
NSString *myText = [mWebView stringByEvaluatingJavaScriptFromString:@"document.documentElement.textContent"];
if ([myText rangeOfString:@"@"].location == NSNotFound) {
NSLog(@"Couldn't find mail address!");
}
else {
NSLog(@"There is a valid mail address!");
}
但我不知道如何将此内容( @ )转换为var。
答案 0 :(得分:2)
试试这个。
NSString *myText = [www stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML;"];
NSString *expression = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression options:NSRegularExpressionCaseInsensitive error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:myText options:0 range:NSMakeRange(0, [myText length])];
if (match){
NSString *email = [myText substringWithRange:match.range];
NSLog(@"There is a valid mail address: %@", email);
}else{
NSLog(@"Couldn't find mail address!");
}