Home > Back-end >  Regex Omit and Validate
Regex Omit and Validate

Time:09-02

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()

CodePudding user response:

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>$

CodePudding user response:

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

  • Related