Ano*_*s-E 5 regex swift swift4
我想清除HTML String文本中的所有属性.我已经找到了很多答案,但问题是如果我们没有正确的CSS风格,正则表达式的语法不起作用.我的情况很困难,因为从API获取的HTML文本与其样式不一致.它可能是这样的:
<p style="\"text-align:" justify;="" \"=""><span style="\"font-size:" 13px;="" font-family:="" arial;="" text-decoration-skip-ink:="" none;\"=""><b><span style="font-size: 18px;">Angkor Wat</span></b> is a temple complex in Cambodia and the largest religious monument in the world, on a site measuring 162.6 hectares (1,626,000 m2; 402 acres). It was originally constructed as a Hindu temple dedicated to the god Vishnu for the Khmer Empire, gradually transforming into a Buddhist temple towards the end of the 12th century. It was built by the Khmer King Suryavarman II in the early 12th century in Ya?odharapura, the capital of the Khmer Empire, as his state temple and eventual mausoleum. Breaking from the Shaiva tradition of previous kings, Angkor Wat was instead dedicated to Vishnu. As the best-preserved temple at the site, it is the only one to have remained a significant religious centre since its foundation. The temple is at the top of the high classical style of Khmer architecture. It has become a symbol of Cambodia, appearing on its national flag, and it is the country\'s prime attraction for visitors.</span></p><p style="\"text-align:" justify;="" \"=""><span style="\"font-size:" 13px;="" font-family:="" arial;="" text-decoration-skip-ink:="" none;\"="">Angkor Wat combines two basic plans of Khmer temple architecture: the temple-mountain and the later galleried temple. It is designed to represent Mount Meru, home of the devas in Hindu mythology: within a moat and an outer wall 3.6 kilometres (2.2 mi) long are three rectangular galleries, each raised above the next. At the centre of the temple stands a quincunx of towers. Unlike most Angkorian temples, Angkor Wat is oriented to the west; scholars are divided as to the significance of this. The temple is admired for the grandeur and harmony of the architecture, its extensive bas-reliefs, and for the numerous devatas adorning its walls.</span></p>
Run Code Online (Sandbox Code Playgroud)
您可以通过复制并将整个文本粘贴到此网站来测试此字符串,因为我想找到可以删除所有CSS样式的正确Regex.
我想要像这个有用的HTML清洁网站一样工作的正则表达式
这是在清理HTML之前:
这是在清理HTML之后:
这些网站可以清除所有HTML属性,并不关心这些属性是否格式错误
我在网站上发现了很多可以清理html属性的正则表达式,但它不符合我的情况:这里有一些正则表达式:
<[^>]+((style|class)="[^"]*")[^>]*><\s*([a-z][a-z0-9]*)\s.*?>style=\"([^\"]*)\"style="(.*?)"<\\s*([a-z][a-z0-9]*)\\s.*?>编辑 这里是一个有用的功能,可以从Tobi中删除样式:
let regex = try! NSRegularExpression(pattern: "style=\"([^\"]*)\"", options: .caseInsensitive)
let range = NSMakeRange(0, html.characters.count)
let modString = regex.stringByReplacingMatches(in: html, options: [], range: range, withTemplate: "")
Run Code Online (Sandbox Code Playgroud)
这个正则表达式的结果仍然是这样的:
<p text-align:" justify;="" \"=""><span font-size:" 13px;="" font-family:="" arial;="" text-decoration-skip-ink:="" none;\"=""><b><span >Angkor Wat</span></b> is a temple complex in Cambodia and the largest religious monument in the world, on a site measuring 162.6 hectares (1,626,000 m2; 402 acres). It was originally constructed as a Hindu temple dedicated to the god Vishnu for the Khmer Empire, gradually transforming into a Buddhist temple towards the end of the 12th century. It was built by the Khmer King Suryavarman II in the early 12th century in Ya?odharapura, the capital of the Khmer Empire, as his state temple and eventual mausoleum. Breaking from the Shaiva tradition of previous kings, Angkor Wat was instead dedicated to Vishnu. As the best-preserved temple at the site, it is the only one to have remained a significant religious centre since its foundation. The temple is at the top of the high classical style of Khmer architecture. It has become a symbol of Cambodia, appearing on its national flag, and it is the country\'s prime attraction for visitors.</span></p><p text-align:" justify;="" \"=""><span font-size:" 13px;="" font-family:="" arial;="" text-decoration-skip-ink:="" none;\"="">Angkor Wat combines two basic plans of Khmer temple architecture: the temple-mountain and the later galleried temple. It is designed to represent Mount Meru, home of the devas in Hindu mythology: within a moat and an outer wall 3.6 kilometres (2.2 mi) long are three rectangular galleries, each raised above the next. At the centre of the temple stands a quincunx of towers. Unlike most Angkorian temples, Angkor Wat is oriented to the west; scholars are divided as to the significance of this. The temple is admired for the grandeur and harmony of the architecture, its extensive bas-reliefs, and for the numerous devatas adorning its walls.</span></p>
Run Code Online (Sandbox Code Playgroud)
请使用本网站测试我的给定字符串
此正则表达式可以只清除风格格式风格=""只
小智 5
您可以使用SwiftSoup来帮助您解决这个问题。这是我的代码
do {
let doc: Document = try SwiftSoup.parse(html)
let elements = try doc.getAllElements()
try elements.forEach { (el) in
let attr = el.getAttributes()
try attr?.forEach({ (att) in
try el.removeAttr(att.getKey())
})
}
print(try doc.body()?.html())
} catch Exception.Error(let type, let message) {
print(type,message)
} catch {
print("error")
}
Run Code Online (Sandbox Code Playgroud)
这是结果
<p><span><b><span>Angkor Wat</span></b> is a temple complex in Cambodia and the largest religious monument in the world, on a site measuring 162.6 hectares (1,626,000 m2; 402 acres). It was originally constructed as a Hindu temple dedicated to the god Vishnu for the Khmer Empire, gradually transforming into a Buddhist temple towards the end of the 12th century. It was built by the Khmer King Suryavarman II in the early 12th century in Ya?odharapura, the capital of the Khmer Empire, as his state temple and eventual mausoleum. Breaking from the Shaiva tradition of previous kings, Angkor Wat was instead dedicated to Vishnu. As the best-preserved temple at the site, it is the only one to have remained a significant religious centre since its foundation. The temple is at the top of the high classical style of Khmer architecture. It has become a symbol of Cambodia, appearing on its national flag, and it is the country\'s prime attraction for visitors.</span></p>\n<p><span>Angkor Wat combines two basic plans of Khmer temple architecture: the temple-mountain and the later galleried temple. It is designed to represent Mount Meru, home of the devas in Hindu mythology: within a moat and an outer wall 3.6 kilometres (2.2 mi) long are three rectangular galleries, each raised above the next. At the centre of the temple stands a quincunx of towers. Unlike most Angkorian temples, Angkor Wat is oriented to the west; scholars are divided as to the significance of this. The temple is admired for the grandeur and harmony of the architecture, its extensive bas-reliefs, and for the numerous devatas adorning its walls.</span></p>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助你:)
| 归档时间: |
|
| 查看次数: |
681 次 |
| 最近记录: |