JavaCoder
JavaCoder

Reputation:

Strip Html from Text in JavaScript except p tags?

I need to change RichEditor and TextEditor modes with JavaScript, now I need to convert Html to Text which is actually still in Html editor mode, so I need just p tags, but other Html can be stripped off.

Upvotes: 3

Views: 2600

Answers (3)

LanreSmith
LanreSmith

Reputation: 161

var input = 'b<p on>b <p>good p</p> a<a>a h1<h1>h1 p<pre>p img<img src/>img';
var output = input.replace(/(<(?!\/?p\s*>)([^>]+)>)/ig, '');
console.log(output);
output: bb <p>good p</p> aa h1h1 pp imgimg

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

Regex replace (globally, case-insensitively):

</?(?:(?!p\b)[^>])*>

with the empty string.

Explanation:

<          # "<"
/?         # optional "/" 
(?:        # non-capture group
  (?!      #   negative look-ahead: a position not followed by...
    p\b    #     "p" and a word bounday
  )        #   end lock-ahead
  [^>]*    #   any char but ">", as often as possible
)          # end non-capture group
>          # ">"

This is one of the few situations where applying regex to HTML can actually work.

Some might object and say that the use of a literal "<" within an attribute value was actually not forbidden, and therefore would potentially break the above regex. They would be right.

The regex would break in this situation, replacing the underlined part:

<p class="foo" title="unusual < title">
                              ---------

If such a thing is possible with your input, then you might have to use a more advanced tool to do the job - a parser.

Upvotes: 11

Rafael
Rafael

Reputation: 18522

This should help

var html = '<img src=""><p>content</p><span style="color: red">content</span>';
html.replace(/<(?!\s*\/?\s*p\b)[^>]*>/gi,'')

explanation for my regex:

replace all parts

  1. beginning with "<",
  2. not followed by (?!
    • any number of white-space characters "\s*"
    • optional "/" character
    • and tag name followed by a word boundary (here "p\b")
  3. containing any characters not equal ">" - [^>]*
  4. and ending with ">" character

Upvotes: 3

Related Questions