美文网首页
iOS html代码中相对路径替换为绝对路径

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

作者: 雨落夏未停 | 来源:发表于2020-05-19 22:59 被阅读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]];
}

相关文章

网友评论

      本文标题:iOS html代码中相对路径替换为绝对路径

      本文链接:https://www.haomeiwen.com/subject/bhxwohtx.html