html - 如何快速删除字符串中的所有html

标签 html swift

考虑这个字符串值:

LCD Soundsystem was the musical project of producer <a href="http://www.last.fm/music/James+Murphy" class="bbcode_artist">James Murphy</a>, co-founder of <a href="http://www.last.fm/tag/dance-punk" class="bbcode_tag" rel="tag">dance-punk</a> label <a href="http://www.last.fm/label/DFA" class="bbcode_label">DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href="http://www.last.fm/tag/alternative%20dance" class="bbcode_tag" rel="tag">alternative dance</a> and <a href="http://www.last.fm/tag/post%20punk" class="bbcode_tag" rel="tag">post punk</a>, along with elements of <a href="http://www.last.fm/tag/disco" class="bbcode_tag" rel="tag">disco</a> and other styles. <br />

如何在 Swift 中删除所有 html 标签?

所以结果必须是:

LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles. 

最佳答案

您可以使用正则表达式,注意我创建的那个:

    var str = "LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"



    let regex:NSRegularExpression  = NSRegularExpression(
        pattern: "<.*?>",
        options: NSRegularExpressionOptions.CaseInsensitive,
        error: nil)!


    let range = NSMakeRange(0, countElements(str))
    let htmlLessString :String = regex.stringByReplacingMatchesInString(str,
        options: NSMatchingOptions.allZeros,
        range:range ,
        withTemplate: "")


    println(htmlLessString)

它转换:

"LCD Soundsystem was the musical project of producer <a href='http://www.last.fm/music/James+Murphy' class='bbcode_artist'>James Murphy</a>, co-founder of <a href='http://www.last.fm/tag/dance-punk' class='bbcode_tag' rel='tag'>dance-punk</a> label <a href='http://www.last.fm/label/DFA' class='bbcode_label'>DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href='http://www.last.fm/tag/alternative%20dance' class='bbcode_tag' rel='tag'>alternative dance</a> and <a href='http://www.last.fm/tag/post%20punk' class='bbcode_tag' rel='tag'>post punk</a>, along with elements of <a href='http://www.last.fm/tag/disco' class='bbcode_tag' rel='tag'>disco</a> and other styles. <br />"

"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles."

唯一的问题是我已将所有双引号 (") 转换为单引号,然后应用正则表达式,否则我需要使用 "\"

将它们全部转义

更新:

我还尝试使用 "\" 转义所有双引号,结果仍然相同:

我使用的新字符串是:

"LCD Soundsystem was the musical project of producer <a href=\"http://www.last.fm/music/James+Murphy\" class=\"bbcode_artist\">James Murphy</a>, co-founder of <a href=\"http://www.last.fm/tag/dance-punk\" class=\"bbcode_tag\" rel=\"tag\">dance-punk</a> label <a href=\"http://www.last.fm/label/DFA\" class=\"bbcode_label\">DFA</a> Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of <a href=\"http://www.last.fm/tag/alternative%20dance\" class=\"bbcode_tag\" rel=\"tag\">alternative dance</a> and <a href=\"http://www.last.fm/tag/post%20punk\" class=\"bbcode_tag\" rel=\"tag\">post punk</a>, along with elements of <a href=\"http://www.last.fm/tag/disco\" class=\"bbcode_tag\" rel=\"tag\">disco</a> and other styles. <br />"

和结果:

"LCD Soundsystem was the musical project of producer James Murphy, co-founder of dance-punk label DFA Records. Formed in 2001 in New York City, New York, United States, the music of LCD Soundsystem can also be described as a mix of alternative dance and post punk, along with elements of disco and other styles."

关于html - 如何快速删除字符串中的所有html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26557358/

相关文章:

javascript - Accordion 上下箭头

javascript - 默认光标在 IE 和 Mozilla 中不起作用

swift - 将阴影转换到 ARKit 中的 SCNNode

swift - ARKit SKVideoNode在渲染上播放

ios - Swift/SKStoreProductViewController pushViewController 结果为 (lldb)

html - 为什么这个图片库超过了它的 div?

html - 我怎么知道为什么忽略 CSS 样式?

html - 有没有办法在html5中同步播放多个音频文件?

Swift, Collection View 和 Storyboard,在另一个标签上使用 viewWithTag 时获取 null

ios - 如何在 Swift native 代码中从 Flutter 调用参数?