Accept Spanish Unicode Chars in JS Regex [duplicate] - javascript

This question already has an answer here:
Allow alphanumeric with spanish regex in javascript?
(1 answer)
Closed 1 year ago.
I have the following regex set up to accept words and some special characters:
const regex = /^[\w\-'.,?\/()\[\]!&\s]+$/;
I want to extend this to also include the range of special characters in Spanish: ñáéíóú
I found this answer which provides a regex for all special chars, but I'm not sure how to incorporate this kind of solution into my already existing regex.

You can simply add those characters to the class you already have in your regex:
const regex = /^[\wñáéíóú\-'.,?\/()\[\]!&\s]+$/;
It is not needed to add the u modifier.
NB: it is not really necessary to escape the [ character inside a character class.

Related

Unable to add unicode flag to regex for email check [duplicate]

This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 3 years ago.
I have following regex string to check for valid email formats
/^(([^<>()[\]\\.,;:\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,}))$/
At the very end of it I want to add unicode flag u so it will look like this
/^(([^<>()[\]\\.,;:\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,}))$/u
However I am getting error saying that regex becomes invalid with unicode flag. Is there any possibility to set it here?
There are multiple solutions in order to validate unicode characters, but this flag cannot be used like that. The \u flag is most used to be followed by a char code like \u00C0.
I think the most reliable solution is to specify the range of accepted unicode characters in the regex.
Something like this should work:
/^(?!\.)((?!.*\.{2})[a-zA-Z0-9\u00E0-\u00FC.!#$%&'*+-/=?^_`{|}~\-\d]+)#(?!\.)([a-zA-Z0-9\u00E0-\u00FC\-\.\d]+)((\.([a-zA-Z]){2,63})+)$/
The solution applied here is to support characters from à to ü.
Regex tester: https://www.regexpal.com/?fam=108260
Related question for mathching unicode characters: Matching accented characters with Javascript regexes

RegExp expression to Accept Alphanumeric values with all Special Charac [duplicate]

This question already has answers here:
Regular expression for all printable characters in JavaScript
(5 answers)
Closed 5 years ago.
I am trying to write a Reg Expression to check if the Language is English or Arabic.
My field under test is used to capture SMS messages. The message can be in
English with numbers & special characters
OR
Arabic with English numbers/Arabic numbers & special characters
Search should be on multiline accepting space & enter.
Check is to allocate the numbers of characters permissible per language. Eg: English allows 160; while Arabic allows only 70 per SMS
I assume the Exp should only check the words (first few to decide the language)
here is a sample of what I wrote in JavaScript; Regex did not work, only RegExp :
var pat = new RegExp("^[A-Za-z0-9\s!##$%^&*()_+=-`~\\\]\[{}|';:/.,?><]*$");
But for the below string it fails :
"Hello & Hi"
Any suggestions?
var regex=/^[ -~]+$/;
var str='Hello & Hi';
console.info(regex.test(str));
you can write like this too
Since you are creating the regular expression from string you need to escape \ character to use \s. Also, you should either escape - or put it just before closing ] when you are not using it to define a range of characters.
var re = new RegExp("^[A-Za-z0-9\\s!##$%^&*()_+=\-`~\\\]\[{}|';:/.,?><]*$");
console.log(re.test("Hello & Hi"));

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

Find a caret ^ character in a string with Javascript match [duplicate]

This question already has answers here:
How do you match a caret (^) symbol in regex?
(2 answers)
Closed 5 years ago.
Will the match method in Javascript find a ^ caret character?
This is not working for me.
var theString = '^A^B^C^D';
var theMatch = theString.match(/^/g);
The ASCII code for the caret is 94. Can I match it by the ASCII code?
^ is a special character. You must escape it:
var theMatch = theString.match(/\^/g);
To complement #syntax excellent response. Please note that some characters are like "reserved keywords" in Regular expressions and any time you need to use them you will have to use \ followed by the character some other examples are \. \$ \[ \( and many others.
If need some additional help with regular expression I would like to recommend you a site that does an excellent job reading at your regular expression and this can help you understand them better:
http://regex101.com/

Categories