2

I would like to use RegExp in JS to replace any html tags which only include whitespaces(' ' or  ) inside of it with its html encoded equivalent  .

For example:

Replace

'<strong> &nbsp; &nbsp; </strong>' => '&nbsp; &nbsp;'

Another example,

Replace:

'<strong>&nbsp; &nbsp; &nbsp;</strong> => '&nbsp; &nbsp; &nbsp;'
2
  • 1
    We can't read your post. Please review formatting. Commented Oct 23, 2015 at 13:11
  • possibly this is related? stackoverflow.com/questions/1732348/… Commented Oct 23, 2015 at 13:15

2 Answers 2

3

You can use a regular expression like this:

str = str.replace(/<(\w+)>((?:&nbsp;|\s)+)<\/\1>/g, '$2');

Explanation:

<(\w+)>          matches the start tag and captures the name
(                group to capture the content
(?:&nbsp;|\s)+   matches &nbsp; or whitespace, one or more times
)                ends group
<\/\1>           matches the end tag with the name of the start tag

The match is replaced by $2, i.e. the value in the second group, i.e. the content inside the tag.

Sign up to request clarification or add additional context in comments.

Comments

0

Lets say you have the string

var string = "<strong>&nbsp; &nbsp; &nbsp; </strong> <strong>no</strong>";

To match the only the spaces and the &nbsp; you can use

var result = string.replace(/<[^>]+>((&nbsp;| )+)<\/[^>]+>/gm, "$1");

Let me explain, <[^>]+> matches any tag and <\/[^>]+> any end tag. the (&nbsp;| )+ matches an space or an   many times

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.