JavaScript Regex - Must contain at least two letters in UTF-8

I am currently trying to create a JavaScript regular expression using the following conditions:

  • Accepts all UTF-8 characters
  • A space between words is allowed as soon as 1 space, not several.
  • Allow all characters, for example :! # $% ^ & * () _- = {} [] except: "@"
  • No spaces after or before the line.
  • The range must be 2-16 characters, including spaces
  • And must contain at least two alphabetic characters per line.

Here is the Regex I have compiled so far:

/^(?![^@]*@)(?![^]*\s\s)\S[^]{0,14}\S$/

Until now, this has done all of the above, except that it does not meet the last condition, which consists in the fact that it must contain at least two letters. For example:

"..He" //should be true

"He$*" //should be true

".." //should be false

"*%" //should be false

"!#$%^&*()" //should be false since there is no letters

"$$tonyMoney™" //should be true

"أنا أحب جاف™" //should be true

"To" //should be true

Any help is appreciated! Thanks!

+4
1

XRegExp:

var rx = XRegExp("^(?![^@]*@)(?![^]*\\s\\s)(?=(?:\\P{L}*\\p{L}){2})\\S[^]{0,14}\\S$");

:

  • ^ -
  • (?![^@]*@) - no @
  • (?![^]*\\s\\s) -
  • (?=(?:\\P{L}*\\p{L}){2}) - 2 Unicode
  • \\S[^]{0,14}\\S - 2 16 , .
  • $ -

regex

var rx = XRegExp("^(?![^@]*@)(?![^]*\\s\\s)(?=(?:\\P{L}*\\p{L}){2})\\S[\\s\\S]{0,14}\\S$");
document.body.innerHTML = rx.test("..He") + " - must be true<br/>";
document.body.innerHTML += rx.test("He$*") + " - must be true<br/>"; 
document.body.innerHTML += rx.test("..") + " - must be false<br/>"; 
document.body.innerHTML += rx.test("*%") + " - must be false<br/>"; 
document.body.innerHTML += rx.test("!#$%^&*()") + " - must be false<br/>"; 
document.body.innerHTML += rx.test("$$tonyMoney™") + " - must be true<br/>"; 
document.body.innerHTML += rx.test("To") + " - must be true<br/>"; 
document.body.innerHTML += rx.test("أنا أحب جاف™") + " - must be true<br/>";
<script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-all-min.js"></script>
Hide result
+1

Source: https://habr.com/ru/post/1618937/


All Articles