skip to Main Content

I need the following:

  • No numbers
  • Only allow the special characters - and '
  • Spaces should not be allowed at the beginning or end of the field
  • Remove any additional blank space if found between parts of names (allow only one space between parts of name – if multiple middle names)
  • Allow diacritics

Chat GPT gave the following:

^(?!.*d)[p{L}'-]+$

However this still allows me to enter consecutive spaces.

2

Answers


  1. Chosen as BEST ANSWER

    OK, found the answer:

    ^(?!.*d)[p{L}'-]+(?:s[p{L}'-]+)*$
    

  2. I think you could do something like that in JavaScript:

    const myString = "Hello    world!  This   is pizza?";
    
    const regex = /^(?!.*d)[p{L}'-]+$/g;
    const result = myString.replace(regex).replace(/s+/g, ' ');
    
    console.log(result);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search