LoginSignup
4
4

More than 5 years have passed since last update.

How to strip tags in Javascript?

Last updated at Posted at 2013-07-12

The 'strip_tags' method is from php, but how to do it in javascript?

There's two solution, choose one of these according to your requirement.

Solution 1

If you simply needs to output plain text.

You can use regular expression to strip all tags from string like the code below.

var stripped = original.replace(/(<([^>]+)>)/ig,"")

Solution 2

If you have to output some simple font tags like '<p><i><b>'.

Using the strip_tags function of php.js which is a website for porting language from php to js.

After you include the library from php.js, you can write some sample like these.

//strip_tags(inputString, allowTags)
strip_tags("<script>alert('test')</script><p>Hello, world.</p>", "<p>")

result will output:

alert('test')<p>Hello, world.</p>

Hope these help. :)

4
4
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4