AVR*_*AVR 1 regex objective-c ios swift swift5
在一种情况下,我需要检查 api 响应是 HTML 格式还是 JSON 格式。首先,我将数据转换为字符串并检查它是否为 HTML 格式?是否有任何正则表达式可用于检查字符串是否为 html 字符串?或者还有其他方法吗?
小智 5
检查字符串是否为 HTML 字符串:
func isValidHtmlString(_ value: String) -> Bool {
if value.isEmpty {
return false
}
return (value.range(of: "<(\"[^\"]*\"|'[^']*'|[^'\">])*>", options: .regularExpression) != nil)
}
let testString = "<p> test html string </p>"
print("\(isValidHtmlString(testString))")
Run Code Online (Sandbox Code Playgroud)
参考: https: //www.geeksforgeeks.org/how-to-validate-html-tag-using-regular-expression/
检查字符串是否为 JSON 字符串:
let jsonString = "{}"
if JSONSerialization.isValidJSONObject(jsonString.data(using: .utf8)!) {
print("valid")
} else {
print("invalid")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3063 次 |
| 最近记录: |