Search code examples
javascripthtmlcssregexarabic

Arabic words handling with Regex in JavaScript


I am trying to handle Arabic strings.

I want to handle multiple spaces between two strings (i.e. first name, last name).

But the RegEx that I am using is valid only for 1 spacing between the first name and last name.

RegEx used:

/^[\u0600-\u06FF]+([ ][\u0600-\u06FF]+)?$/

Please suggest.


Solution

  • As suggested by Simone Chelo, you need to add "+" to the regex. It means "one or more". You also don't need to wrap the space with brackets. This should work for you:

    /^[\u0600-\u06FF]+( +[\u0600-\u06FF]+)?$/
    

    If you want any kind of white space, you can use \s instead of [ ]

    /^[\u0600-\u06FF]+(\s+[\u0600-\u06FF]+)?$/
    

    Here is a great resource for regex.