How to match the character class [:alpha:] in JavaScript [duplicate] - javascript

This question already has answers here:
How can I use Unicode-aware regular expressions in JavaScript?
(11 answers)
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
I need to match all alphabetic characters (not only [a-zA-Z], but really all of them including ö, ß, â, ç, Æ, Å, Ĺ, Ĩ, Ÿ, Ș, њ, ѝ, Ц, ت, ר). In other programming languages there is character class named [:alpha:] for this, because it is virtually impossible to name all alpha characters from all alphabets in brackets.
\w doesn't help because it includes digits and underscore. I need letters only without digits, punctuation, spaces.

If you can use XRegExp, use. It has support for Unicode. Otherwise you have to enumerate the ranges yourself.

Related

RegEx validation Issue for allowing multiple whitespaces [duplicate]

This question already has answers here:
Learning Regular Expressions [closed]
(1 answer)
Match empty string, comma, hyphen or underscore once using regex
(2 answers)
Closed 4 years ago.
I have a requirement to do a field validation using regEx. The requirement is not to have any special characters. It is working for me for a single white space, but not for multiple white spaces. It does not work if there is a white space at the beginning. Can you please help me with this?
This is what I used:
^(\w+ ?)*$
You might be looking for
^[\w ]*$
The ^ and $ are anchors for the start/end of the string, the [...] is called a character class and would allow only [A-Za-z0-9_ ]. The * is a quantifier and means zero or more times, thus the expression would also allow an empty string. If this is not what you want, change it to + instead of the *. Please note that this would also allow a string with only spaces (it really depends on what you want).

Regex which allows alphabets and spaces combination but not only spaces [duplicate]

This question already has answers here:
Regular expression that allows spaces in a string, but not only blank spaces
(6 answers)
Closed 5 years ago.
I want to know the regular expression which allows the combination of alphabets and space but it shouldn't allow only the spaces.
I have searched and read all the articles and found the expression /^[a-zA-Z\s]*$/ which allows only spaces also along with the combination of alphabets and space.
Thanks in advance.
Make your expression require one alphabet and then the additional alphabets and spaces are optional:
/^[A-Za-z]|[A-Za-z][A-Za-z\s]*[A-Za-z]$/
Also that \s means any whitespace characters (tabs, newlines, carriage returns etc), not just space.
(edited: my earlier answer was flawed. This new one ensures input is either just one alphabet, or has to start and end with alphabet with spaces allowed only in between.

How to use a slash in javascript RegExp [duplicate]

This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 6 years ago.
How do I make a Slash able to be used in this Metachar String:
/#(\w+)\b/gi
That is supposed to find the "Text"(#text) This is a test #Text I agree
And it does. But now I wan't the same thing for somthing that uses a
/
You need to escape the slash so it is not interpreted as denoting special meaning. Escaping means prefixing with a backslash, so you just need two together. Adapting your existing example:
/#([\w\/]+)\b/gi
You're now allowing alphanumeric and slash characters (hence the need for a "range" of characters, denoted by square brackets.)
This one will do it: Try:(will match /Text)
/\/(\w+)\b/gi

Is it possible to let \w regexp pattern to allow characters such as é as well? If not, what alternatives are there? [duplicate]

This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 8 years ago.
Lets say I have a regexp that looks like:
\w+
Then this string would pass:
helloworld
However this won't:
héllowörld
It will stop at é (and theöwill break it as well) even though for a human héllowörld doesn't sound so far fetched as a single word.
Is there a way I can improve \w so it will also include special word characters? Or do I have to append every special latin character into my regexp like this into:
[\wéèåöä...........]+
Because that doesn't seem like the best option to try and figure out what all the different special latin characters there are in the world that would be reasonable.
What options do I have?
\w match any word character [a-zA-Z0-9_]. It doesn't match non-english character.
Read this post for Regular expression to match non-english characters?
Sometimes I use an inverse method to match non-english among the other characters. Check this out
var string = "你好 κόσμος привет šđčߣłćž çë asgfgrtzj 657 #$%&/()=?*!";
The pattern below
var pattern = /([^0-9]+)/gi;
will exclude all numbers
你好 κόσμος привет šđčߣłćž çë asgfgrtzj #$%&/()=?*!";
adding special characters from the above to the pattern
var pattern = /([^0-9#$%&/()=?*!]+)/gi;
the final string would look as following
你好 κόσμος привет šđčߣłćž çë asgfgrtzj

javascript regex for american phone number [duplicate]

This question already has answers here:
How to validate phone numbers using regex
(43 answers)
Closed 8 years ago.
I have this regex expression var re = /(?:\d{3}|\(\d{3}\))([\w-\/\.]?)\d{3}\1\d{4}/;, however, the \w whitespace doesn't work on this test console.log(re.test('123 456 7890'));
Here is my jsfiddle: http://jsfiddle.net/Bqb22/
You shouldn't use \w for whitespace. Use \s instead. (\w is word character, the same as [0-9A-Za-z_], and should not be used to indicate whitespace).

Categories