I have a problem with javascript regex I try to get all word in string text but i need to exclude html tags ..
my regex
/\b([\S]+)\b/g
but for example <br> is not exclude ..
An example here https://regex101.com/r/oT9uC1/4
Thx all
I have a problem with javascript regex I try to get all word in string text but i need to exclude html tags ..
my regex
/\b([\S]+)\b/g
but for example <br> is not exclude ..
An example here https://regex101.com/r/oT9uC1/4
Thx all
The easiest way is to strip out the tags first,
then run your regex on the newtext.
newtext = text.Replace( /<(?:script(?:\s+(?:"[\S\s]*?"|'[\S\s]*?'|[^>]*?)+)?\s*>[\S\s]*?<\/script\s*|(?:\/?[\w:]+\s*\/?)|(?:[\w:]+\s+(?:(?:(?:"[\S\s]*?")|(?:'[\S\s]*?'))|(?:[^>]*?))+\s*\/?)|\?[\S\s]*?\?|(?:!(?:(?:DOCTYPE[\S\s]*?)|(?:\[CDATA\[[\S\s]*?\]\])|(?:--[\S\s]*?--)|(?:ATTLIST[\S\s]*?)|(?:ENTITY[\S\s]*?)|(?:ELEMENT[\S\s]*?))))>/g, '');
I would try and do a regex replace on html tags instead of trying to find all the text.
so use something like this:
var str = "Non ! Non ! Je ne veux pas d'un éléphant!<br> dans un boa. Un boa c'est très dangereux, et un éléphant c'est très encombrant. Chez moi c'est tout petit. J'ai besoin d'un mouton. Dessine-moi un mouton.";
var res = str.replace(/<.+>/g, "");
You could obviously check for the br tag specifically and replace with newline characters.
This should then strip out all html tags leaving you with just the raw text instead.
Also, it is a good idea to keep in mind, that if you remove something, you need to make sure what you leave behind would not leave runnable code.
see this for an example: Stripping script tags from HTML input