Skip to content

Instantly share code, notes, and snippets.

@0x30
Created December 20, 2017 08:00
Show Gist options
  • Save 0x30/4ee9c9c2e958088830b226d5e7d67452 to your computer and use it in GitHub Desktop.
Save 0x30/4ee9c9c2e958088830b226d5e7d67452 to your computer and use it in GitHub Desktop.
使用 STRegex 完成 jsonp 完成数据的解析, 将字符串完成 gb2312 编码解析
//
// String+Regex.swift
// Rate
//
// Created by 荆文征 on 2017/11/20.
// Copyright © 2017年 s. All rights reserved.
//
import Regex
import Foundation
extension String {
/// JSONP 字符串 抽取其中的 JSON 字符串
///
/// - Returns: JSON 字符串
func jsonpRegex() -> String? {
if let string = Regex("[a-zA-Z]*\\(([\\s\\S]*)\\)").firstMatch(in: self)?.captures[0] {
var nString = string
nString.replaceAll(matching: "([\\{|,])([a-zA-Z]*):", with: "$1'$2':")
nString.replaceAll(matching: "'", with: "\"")
return nString
}
return self
}
/// 获取 String.Encoding GB2312 类型
///
/// - Returns: 返回 2312 编码
static func gb2312() -> String.Encoding {
return String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(0x0632))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment