Reputation: 4077
I have a function that through regular expression removes html content:
a.replace( /<.*?>/g, "");
However, if there are spaces they remain, for example:
<a href='site.com'> testing</a>
That will keep the spaces. Also for something like this:
<a href='site.com'> $20</a>
I would like the function to return only 20. So, the question is:
How do I modify the regular expression so that $ and spaces get removed as well?
Upvotes: 0
Views: 110
Reputation: 3333
You could extend your expression and use:
a.replace( /(?:\s|\$)*<.*?>(?:\s|\$)*/g, "");
Now, (?:\s|\$)
was added. This forms a pattern of whitespaces (\s
) or the $ sign (\$
). The escape before the $ sign is necessary since it would match line ends otherwise. Putting ?:
directly after the parenthesis creates a group for searching that is not returned as a group result.
The pattern occurs twice to allow removal of whitespace or $ signs before or after the tag.
Upvotes: 3
Reputation: 16825
or to also remove the whitespace and dollar if there is no html tag present.
a.replace( /(<.*?>)|([\s$])/g, "");
Upvotes: 0