Hasan Teoman Tıngır
Hasan Teoman Tıngır

Reputation: 2901

Find and replace white spaces in html

I'm trying to find and replace all white spaces in a html file. Here's my code so far:

html.replace(@ (?![^<]*>|[^<>]*</)@g,"&nbsp;")

But above expression throws error as not a valid expression. How do I make it work?

Upvotes: 4

Views: 131

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44145

Regexes are delimited by /, not @.

html.replace(/ (?![^<]*>|[^<>]*<\/)/g,"&nbsp;")

Here's another regex that literally replaces every white space with a non-breaking space:

html.replace(/\s+/g, "&nbsp;");

Upvotes: 3

Related Questions