Arabic words handling with Regex in JavaScript - 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.

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.

Related

javascript regular expression allow name with one space and special Alphabets

how to write regular expression allow name with one space and special Alphabets?
I tried with this [a-zA-Z]+(?:(?:\. |[' ])[a-zA-Z]+)* but not working for me,
example string Björk Guðmundsdóttir
You may try something along these lines:
^(?!.*[ ].*[ ])[ A-Za-zÀ-ÖØ-öø-ÿ]+$
The first negative lookahead asserts that we do not find two spaces in the name. This implies that at most one space is present (or no spaces at all). Then, we match any number of alphabets, with most accented letters included. Spaces can also be matched, but the lookahead would already ensure that at most one space can be present.
Demo
Use this one:
[a-zA-Z\u00C0-\u00ff]*[ ]{1}[a-zA-Z\u00C0-\u00ff]*
Answer from other question

Custom email regexp

I trying to create custom email regexp
[first]#[second].[third]
Allowed signs are numbers, letters and some special characters (I have done it)
One #
First+second to be from 1 to 20 characters long.
Email can't start or end with dot. Two or more dots next to each other are also not
allowed.
Third string to be 2 or 3 characters long.
This is what I have so far:
^(?!\.)(?=[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+#[a-zA-Z0-9]).{2,21}(?:\.[a-zA-Z0-9].{2,3})$
Unfortunately it doesn't work as I expected. Thanks for any tips.
For instance I can add multiple dots next to each other:
test...s#fm.com
Never ever write a regex to check for e-mails. You'll never make it right. As an example you say:
Allowed signs are numbers, letters and some special characters (I have done it)
which is terribly wrong, because you're then not including unicode characters like å or ţ which are valid. I'm also pretty sure you don't know that # is a valid value within the first part of an e-mail.
Third string to be 2 or 3 characters long.
and what about matching the following tlds: .info, .ninja, .website or .space?
So please, don't.
for reference:
https://davidcel.is/posts/stop-validating-email-addresses-with-regex/
http://stackoverflow.com/questions/23471591/regex-for-iban-allowing-for-white-spaces-and-checking-for-exact-length/23471740#23471740
http://thedailywtf.com/articles/how-to-validate-a-url
Following regex might help you.
^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,3}))$
Please read answer posted by #zmo too.

RegEx for exactly two or three words

Could someone help me out with this?
At first I was trying to figure out how to simply check for input containing one or two words, and I was able to find that that would be with \w* ?\w+ and for containing exactly two words would be with \w+ \w+ And I got to something like this (which is not working):
/^$|^([a-zA-ZčČćĆđĐšŠžŽ -])\w+ \w+$/
What I've since figured out is that it should contain not one or two, but two or three words. And since I was unable to figure out the RegEx for two words to start with, I had to ask for help here.
Like I said, I need it to allow entering only two or three words with no numbers and with the addition of these letters čČćĆđĐšŠžŽ and a -
Also I need it to ignore a blank input, that's why ^$| is there.
I am really, really new at this, so any help would be appreciated.
EXAMPLES:
Marko Marković
John Smith
Mary-Jane Austin
John III Johnson
Just replace your new definition of "word" character to all the \w. This is for exactly 2 words, with exactly 1 space in between:
/^$|^[a-zA-ZčČćĆđĐšŠžŽ-]+ [a-zA-ZčČćĆđĐšŠžŽ-]+$/
For exactly 2 or 3 words:
/^$|^[a-zA-ZčČćĆđĐšŠžŽ-]+ [a-zA-ZčČćĆđĐšŠžŽ-]+( [a-zA-ZčČćĆđĐšŠžŽ-]+)?$/
Note that I have removed the space in your character class, since it shouldn't be considered part of a "word", or your "word" count will mess up.
You can use Unicode regex to filter it out.
[\p{L}\s-]+
\p{L} : This will match any unicode alphabet from any language.
\s : Space character.
- : Dash ( - ).
You can see how it matches here.
For more about unicode regex you can refer this.

JS regex name pattern

I need a little help. I want to create a regex pattern in order to validate names, it should contain only letters (any type of letters, non European included), apostrophes, periods, dashes and whitespaces. Or, to put it in another flavor, the regex should not validate any numbers, [], {}, <> etc. Is there a way to to that?
Thank you in advance.
/(\w|\s|[\.\'-])+/
But that's not enough, I guess. Surely we must consider that an apostrophe can not be in the beginning, that several dashes can not follow in a row, etc.
You need a more precise definition of the name.
The Regex you pasted is flawed, it should be
^([a-zA-Z]|\s)*$
Notice the extra parenthesis
Also, You were on the right track but just put all allowed characters in the character class [] :
^([-\w'.\s])*$
a-zA-Z was replaced by the short hand character class for words \w
Add allowed characters as needed

RegEx string for three letters and two numbers with pre- and post- spaces

Two quick questions:
What would be a RegEx string for three letters and two numbers with space before and after them (i.e. " LET 12 ")?
Would you happen to know any good RegEx resources/tools?
For a good resource, try this website and the program RegexBuddy. You may even be able to figure out the answer to your question yourself using these sites.
To start you off you want something like this:
/^[a-zA-Z]{3}\s+[0-9]{2}$/
But the exact details depend on your requirements. It's probably a better idea that you learn how to use regular expressions yourself and then write the regular expression instead of just copying the answers here. The small details make a big difference. Examples:
What is a "letter"? Just A-Z or also foreign letters? What about lower case?
What is a "number"? Just 0-9 or also foreign numerals? Only integers? Only positive integers? Can there be leading zeros?
Should there be a single space between the letters and numbers? Or any amount of any whitespace? Even none?
Do you want to search for this string in a larger text? Or match a line exactly?
etc..
The answers to these questions will change the regular expression. It would be much faster for you in the long run to learn how to create the regular expression than to completely specify your requirements and wait for other people to reply.
I forgot to mention that there will be a space before and after. How do I include that?
Again you need to consider the questions:
Do you mean just one space or any amount of spaces? Possibly not always a space but only sometimes?
Do you mean literally a space character or any whitespace characters?
My guess is:
/^\s+[a-zA-Z]{3}\s+[0-9]{2}\s+$/
/[a-z]{3} [0-9]{2}/i will match 3 letters followed by a whitespace character, and then 2 numbers. [a-z] is a character class containing the letters a through z, and the {3} means that you want exactly 3 members of that class. The space character matches a literal space (alternately, you could use \s, which is a "shorthand" character class that matches any whitespace character). The i at the end is a pattern modifier specifying that your pattern is case-insenstive.
If you want the entire string to only be that, you need to anchor it with ^ and $:
/^[a-z]{3} [0-9]{2}$/i
Regular expression resources:
http://www.regular-expressions.info - great tutorial with a lot of information
http://rexv.org/ - online regular expression tester that supports a variety of engines.
^([A-Za-z]{3}) ([0-9]{2})$ assuming one space between the letters/numbers, as in your example. This will capture the letters and numbers separately.
I use http://gskinner.com/RegExr/ - it allows you to build a regex and test it with your own text.
As you can probably tell from the wide variety of answers, RegEx is a complex subject with a wide variety of opinions and preferences, and often more than one way of doing things. Here's my preferred solution.
^[a-zA-Z]{3}\s*\d{2}$
I used [a-zA-Z] instead of \w because \w sometimes includes underscores.
The \s* is to allow zero or more spaces.
I try to use character classes wherever possible, which is why I went with \d.
\w{3}\s{1}\d{2}
And I like this site.
EDIT:[a-zA-Z]{3}\s{1}\d{2} - The \w supports numeric characters too.
try this regularexpression
[^"\r\n]{3,}

Categories