JavaScript Regex Extension

I use the regex below to validate against any html tags in the text area. If someone enters any html tags in the text box, I should show a validation message,

Used expression:

/<(\w+)((?:\s+\w+(?:\s*=\s*(?:(?:"[^"]*")|(?:'[^']*')|[^>\s]+))?)*)\s*(\/?)>/ 

Can someone explain the above regex, I check it for any html tags in the text area, It works for all cases, but below CASE is not checked,

</p> passes the check without checking the client I do not want users to even enter the closing HTML tag

+4
source share
2 answers

Try it -

Regx to limit html tags -

 /([\<])([^\>]{1,})*([\>])/i 

or

 /<(.|\n)*?>/g 

Example

+1
source

I would do such a check as follows:

 var d = document.createElement('div'); d.innerHTML = 'whatever </p>'; if (d.getElementsByTagName('*').length) { alert("You have typed some HTML"); } 
+3
source

Source: https://habr.com/ru/post/1489634/


All Articles