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

2020-05-19  本文已影响0人  雨落夏未停

使用本地的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]];
}
上一篇 下一篇

猜你喜欢

热点阅读