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"));
Related
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.
This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
Hi I'm trying to remove the letters and special characters from javascript.
Example:
var id = "Parameter[0].Category"
I only need the "0" from this. Thank you
Try this
var numbers = id.match(/\d+/)[0];
console.log(numbers);
In general this is called filtering using regular expressions.
If you want to filter out letters and special characters you can use regular expression in JS, like this:
id = id.replace(/\D/g, "")
You replace every (g option) character in your string that is not a digit \D with blank ""
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
This question already has answers here:
Including a hyphen in a regex character bracket?
(6 answers)
Closed 4 years ago.
I want to validate user's input and i use the following (which works fine) as regex.
pattern = /^[a-zA-Z0-9 .,!?;-]+$/;
But when i try with all the characters i want, which is this.
pattern = /^[a-zA-Z0-9 .,!?;-:()#'+=/]+$/;
It doesn't work and I dont know why. Also, I would appreciate it a lot if you explained to me what's the difference when I add the ^ and the +$. Also i have tried using \s instead of space, and it still doesn't work(I prefer just space because i want to restrict line change).
The "hyphen-minus" character, -, has special meaning within a character set (a set of characters between [ and ]). It, -, defines a range of characters. Most of the time in a character set that you want the - to represent itself, you need to use \- to escape its special meaning. In your use, - needs to be escaped \- when used between ; and :.
Working:
pattern = /^[a-zA-Z0-9 .,!?;\-:()#'+=/]+$/;
If you actually want the range of characters between : and ;, then you need to specify the one that has a lower character code (Ascii code chart, or Unicode) first. In this case, that means :-; as : comes before ;:
Show :-; range is valid:
pattern = /^[a-zA-Z0-9 .,!?:-;()#'+=/]+$/;
The same error would be generated if you were to try to specify a range such as z-a.
Show same error with range of z-a:
pattern = /^[z-aA-Z0-9 .,!?:-;()#'+=/]+$/;
The - does not take on its special meaning if it is the first or last character in the character set.
Note: In some regular expression implementations, the - does have its special meaning if you try to use it as the last character in the character set. You are better off just being in the habit of escaping it with \-.
Using - as first or last character in character set:
pattern = /^[-a-zA-Z0-9 .,!?;:()#'+=/]+$/;
pattern = /^[a-zA-Z0-9 .,!?;:()#'+=/-]+$/;
Original code with error:
pattern = /^[a-zA-Z0-9 .,!?;-:()#'+=/]+$/;
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