Javascript Regexp to find the combination of < and > in a string

Viewed 68

We have a input text field where need to restrict user to enter combination of < and >. RegExp Used:

var reg = /<[a-z][\s\S]*>/i;

Combinations which is not covering:

<>
</div>
<121343#>
<@34>
<!@#$<>?"|>

I need regexp that validate text string with which has any character/number/special character in-between or before or end with the combination of <>.

Could anyone help me on this?

2 Answers

You can use

<[\s\S]*>

See the regex demo (where [\s\S] is replaced with .* in order to stay on the same line).

Details:

  • < - a < char
  • [\s\S]* - any text, zero or more chars
  • > - a > char.

Another option in JavaScript is using [^] to match any char including newlines.

<[^]*>

Or not crossing newlines

<[^\r\n]*>
Related