0

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

1
  • What regex engine are you using? Commented Feb 22, 2016 at 17:06

2 Answers 2

0

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, '');

Demo

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

Comments

0

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

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.