iOS html代码中相对路径替换为绝对路径

使用本地的js、css、html,但是使用相对路径的src及href。需要把相对路径转换为绝对路径。(如果路径不是/或者.开头的不进行转换)

Swift

func convertHtmlRelativePath(input: String, absolutPath: String) -> String {
    do {
        let regular = try NSRegularExpression(pattern: "(=['|\"])[/|.]+", options: .caseInsensitive)
        return regular.stringByReplacingMatches(in: input, options: .reportCompletion, range: NSRange(location: 0, length: input.count), withTemplate: "$1\(absolutPath)/")
    } catch {
        return input
    }
}

OC

- (NSString *)convertHtmlRelativePath:(NSString *)input absolutPath:(NSString *)absolutPath
{
    NSError *error;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(=['|\"])[/|.]+" options:NSRegularExpressionCaseInsensitive error:&error];
    if (error) {
        return input;
    }
    return [regex stringByReplacingMatchesInString:input options:NSMatchingReportCompletion range:NSMakeRange(0, input.length) withTemplate:[@"$1" stringByAppendingString:absolutPath]];
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。