Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove HTML tag from string

Tags:

ios

swift

swift3

Remove all HTML tags like &nbsp;or <p> from string. I used below code but it's not working.

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

but it does not remove all HTML tags from string.

like image 765
LALIT Avatar asked Jan 26 '17 13:01

LALIT


People also ask

How do you remove tags in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How remove HTML tag from string in react?

//remove html tags from a string, leaving only the inner text function removeHTML(str){ var tmp = document. createElement("DIV"); tmp. innerHTML = str; return tmp. textContent || tmp.

Which function is used to remove all HTML tags from string?

The strip_tags() function strips a string from HTML, XML, and PHP tags. Note: HTML comments are always stripped.


1 Answers

var content = "<p>&nbsp;&nbsp;test result</p><br/>"; // My String

let a = content.replacingOccurrences(of: "<[^>]+>", with: "", options: String.CompareOptions.regularExpression, range: nil)

a will be: &nbsp;&nbsp;test result

let b = a.replacingOccurrences(of: "&[^;]+;", with: "", options: String.CompareOptions.regularExpression, range: nil)

b will now be: test result

This will also take care of &lt; and so on. There is no magic. Find out what you need and then write the proper RegEx.

like image 149
Idan Avatar answered Oct 09 '22 00:10

Idan