regextypescriptxregexp

xregexp having different result


I want to validate string with rules:

so far I have come up with the following regex:

    static personName = XRegExp.cache("^[\\s\\p{L}\\'\\-\\(\\)]+(?=\\S*\\p{L})\\S+$");

which doesn't work correctly. Only "^(?=\\S*\\p{L})\\S+$" this helps with the letters, I struggle to understand how to add symbols to it so that all rules will be passed, what am I doing wrong?


Solution

  • If the chars you allow are restricted to those you enumerated you may use

    var regex = XRegExp("^[\\s'()-]*\\p{L}[\\s\\p{L}'()-]*$"); 
    

    If you want to allow any chars but only a subset of symbols, with "at least 1 letter" restriction use

    var regex = XRegExp("^[\\p{N}\\s'()-]*\\p{L}[\\p{L}\\p{N}\\s'()-]*$"); 
    

    See the JS demo:

    var regex = XRegExp("^[\\s'()-]*\\p{L}[\\s\\p{L}'()-]*$");
    console.log( regex.test("Sóme (unknown-string) doesn't like it") );
    
    var regex = XRegExp("^[\\p{N}\\s'()-]*\\p{L}[\\p{L}\\p{N}\\s'()-]*$"); 
    console.log( regex.test("Sóme unknown-string (123)") );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/xregexp/3.2.0/xregexp-all.min.js"></script>

    Details