5

I have a model string which looks like:

bla bla bla <b>asdad</b> bla bla bla bla <u>bla</u> ...

My model:

public class MyModel {
      [RegularExpression( @"^(<\s*([^ >]+)[^>]*>.*?<\s*\/\s*\1\s*>)$", ErrorMessage = "No tag is allowed !")]
      public string Text { get; set; }
}

I tried to negate above regex ( I know that I didn't use correctly and I don't know how to do this correctly)

I want to show error when Text contains any match of HTML code, even it has no closed tag, means should occur when met:

<b> without </b>

</b> or similar

How to achieve this with regex ?

2
  • I tried that in regex101 and not working mean that any words that are not html tag are not highlighted Commented Feb 29, 2016 at 13:26
  • You'd better make this clear in the title, as well as in this post, not in the comment. That would make it a lot easier for people who are willing to help. Commented Mar 1, 2016 at 14:04

2 Answers 2

5

This is the regex for that:

<(\s*[(\/?)\w+]*)

It checks for even if single closing tag is there or opening tag is there, it matches that.

DEMO here

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

5 Comments

Ok, it is possible to negate above regex?
negate means, it should not match html tags (there should not be html tags in string)?
Yes, exactly...I wish, if possible, to highlight all text that are not between < and > ...
for that you can use like: ^((?![<>]).)*$
this doesn't look reliable- it would match "5 is < 10" as a piece of html
0

The following regex matches if a model string contains no HTML tags:

^((?!\<(|\/)[a-z][a-z0-9]*>).)*$

The demo is HERE

1 Comment

Updated regex. I misunderstood it previously.

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.