Match string (user input) with regular expression

I have a requirement when there is a username text box and the user can enter the client name to search for the client. And this condition, which the user can add, does a search for wild cards * either infront, or after the client’s name. And the customer name must be at least three characters. I use Regex to validate user input. Now, if the input signal is like "* aaa *". I test this input type with the following regular expression:

[*] {1} ([AZ] | [AZ] | [0-9]) {3} [*] {1}

The code is as follows:

    var str = "*aaa*";
    var patt = new RegExp("[*]{1}([a-z]|[A-Z]|[0-9]){3,}[*]{1}");
    var res = patt.test(str);  
    alert(res);
Run codeHide result

    var str = "*aaa***";
    var patt = new RegExp("[*]{1}([a-z]|[A-Z]|[0-9]){3,}[*]{1}");
    var res = patt.test(str);  
    alert(res);
Run codeHide result

    var str = "*aaa*$$$$";
    var patt = new RegExp("[*]{1}([a-z]|[A-Z]|[0-9]){3,}[*]{1}");
    var res = patt.test(str);  
    alert(res);
Run codeHide result

"* aaa *" res . "* aaa **", "* aaa * $" comimg true. , (* aaa *), . ( "* aaa **", * aaa * $** ..) .

, , ? ?

+4
2
^(?:[*]([a-z]|[A-Z]|[0-9]){3,}[*])$

^$, . . .

https://regex101.com/r/tS1hW2/17

+2

*aaa*$$$ *aaa*, true; . $ ^ .

, , . \w [0-9a-zA-Z_], , \w ( \w) ; , ; -)

var str = "*aaa*$";
    var patt = /^\*[^\W_]{3,}\*$/;
    var res = patt.test(str);  
    alert(res); // false
Hide result

:

[A-Za-z0-9]
+1

All Articles