MEAG
MEAG

Reputation: 127

Regex Omit and Validate

I have this string

"<p></p>" fixed

Is it possible to have a regex that would omit the 7 characters and check if there are at least 3 characters in between?

"<p></p>" = false
"<p>   </p>" = false
"<p>Hello</p>" = true 
"<p>Hello </p>" = true

Further details:- By default, my editor has the p tags. Therefore my validator is always returning editor string value is not empty. So I want to add the regex expression in the matches fx.

string().required().matches()

Upvotes: 0

Views: 45

Answers (2)

The fourth bird
The fourth bird

Reputation: 163477

If you want to exclude the opening and closing tags in between as metioned in the comments:

<p>(?=[^<>]{3,}<\/p>)[^<>]*[^\s<>][^<>]*<\/p>

Explanation

  • <p> Match literally
  • (?=[^<>]{3,}<\/p>) Positive lookahead, assert at least 3 chars other than < and > before the closing </p>
  • [^<>]* Match optional chars other than < and >
  • [^\s<>] Match a single none whitespace char other than < and >
  • [^<>]* Match optional chars other than < and >
  • <\/p> Match </p>

See a Regex demo

Upvotes: 1

Luca
Luca

Reputation: 19

I think this is what you look for:

^<p>[\S]{3,}<\/p>$  

if whitespaces are ok for you as well it is this one:

^<p>.{3,}<\/p>$

Upvotes: 2

Related Questions