简体   繁体   中英

Setting regular expression to test input text which contain HTML markup

I am trying to create a pattern to prevent user inputs from HTML mark up. I created this patten

var htmlTagRegex = /^<+[a-z]+>/;

which only works when the input starts with html tag for example:

<br /> Test
<p> Test
<div> Test

but in case of having an input like below

Test <br /> 
Test <p> 
Test <div> 

the pattern not working (probably because of /^ ). How can I write the pattern to consider any html tag contains?

^(?=.*(?:<[^>]+>)).*$

You can use this to detect html markers.See demo.

http://regex101.com/r/lZ5mN8/43

now my question is can you please let me know how to power the pattern to consider any html tag contains?

<\w+\b[^<>]*>

For matching custom tags,

<[^<>]+>

To match the whole line which contains the tag. You don't need to go for lookaround assertions.

^.*?<[^<>]+>.*$

DEMO

Just for your information

In jQuery, If you want extract text alone without html markup , then you can use pattern like this

$("<div/>").html("#elementId").text()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM