I am trying to make regexp for validating string not containing
^ ; , & . < > | and having 1-20 characters. Any other Unicode characters are valid (asian letters for example).
How to do it?
You can use the following:
^[^^;,&.<>|]{1,20}$
Explanation:
^ assert starting of the string
[^ start of negated character class ([^ ])
^;,&.<>| all the characters you dont want to match
] close the negates character class
{1,20} range of matches
$ assert ending of the string
It will match any character other than specified characters within range of 1-20.
Your regex \w[^;,&.<>|]{1,20} contains \w that might not match all Unicode letters (I guess your regex flavor does not match Unicode letters with \w). Anyway, the \w only matches 1 character in your pattern.
Also, you say you need to exclude ^ but it is missing in your pattern.
When you want to validate length, you also must use ^/$ anchors to mark the beginning and end of a string.
To create a pattern for some range that does not match specific characters, you need a negated character class with anchors around it, and the length is set with limiting quantifiers:
^[^^;,&.<>|]{1,20}$
Or (this version makes sure we only match at the beginning and end of the string, never a line):
\A[^^;,&.<>|]{1,20}\z
Note that inside a character class, almost all special characters do not require escaping (only some of them, none in your case). Even the ^ caret symbol.
See demo
Related
I have a regular expression for allowing unicode chars in names(Spanish, Japanese etc), but I don't want to allow '.'(dot) anywhere in the string.
I have tried this regex but it fails when string length is less than 3. I am using xRegExp.
^[^.][\\pL ,.'-‘’][^.]+$
For Example:
NOËL // true
Sanket ketkar // true
.sank // false
san. ket // false
NOËL.some // false
Basically it should return false when name has '.' in it.
Your pattern ^[^.][\\pL ,.'-‘’][^.]+$ matches at least 3 characters because you use 3 characters classes, where the first 2 expect to match at least 1 character and the last one matches 1 or more times.
You could remove the dot from your character class and repeat that character class only to match 1+ times any of the listed to also match when there are less than 3 characters.
^[\p{L} ,'‘’-]+$
Regex demo
Or you could use a negated character class:
^[^.\r\n]+$
^ Start of string
[^.\r\n]+ Negated character class, match any char except a dot or newline
$ End of string
Regex demo
You could try:
^[\p{L},\-\s‘’]+(?!\.)$
As seen here: https://regex101.com/r/ireqbW/5
Explanation -
The first part of the regex [\p{L},\-\s‘’]+ matches any unicode letter, hyphen or space (given by \s)
(?!\.) is a Negative LookAhead in regex, which basically tells the regex that for each match, it should not be followed by a .
^[^.]+$
It will match any non-empty string that does not contain a dot between the start and the end of the string.
If there is a dot somewhere between start to end (i.e. anywhere) it will fail.
I'm trying to strip a string of all characters that are not a letter or a number. I tried String.prototype.replace with a regular expression, but it didn't remove the expected characters:
this.colorPreset1 = this.colorPreset1.replace(/^[0-9a-zA-Z]+$/, '');
this.colorPreset1=this.colorPreset1.replace(/[^0-9a-zA-Z]/g, '');
The character group was changed to a exclusion group. [^] will match any character not in the list. As you had it, it would only match the characters you wanted to keep.
The anchors for the string were removed - You're wanting to replace any non-alpha numeric characters, so it doesn't matter where they're located.
The global flag //g was added so it will replace all matches instead of just the first one.
By adding ^ and $ around your regular expression, you explicitly tell it to match strings starting and ending with this pattern.
So it will replace the searched pattern only if if all the content of the string matches the pattern.
If you want to match each occurence of non numerical or alphabetical characters, you will have to remove the ^ start constraint and the $ end constraint, but also will have to change the pattern itself:
[A-Za-z0-9]
matches alphabetical or numerical characters, you want the opposite of that (to inverse a character class add a ^ at the start of the character class:
[^A-Za-z0-9]
finally add the g option to the regex to tell it to match each occurence (otherwise only the first occurence will be replaced):
/[^A-Za-z0-9]+/g
JavaScript RegEx replace will only replace the first found value. If you specify the g argument in your pattern, it denotes Global or "replace all."
this.colorPreset1=this.colorPreset1.replace(/[^0-9a-zA-Z]/g, '');
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
What does the regular expression /_/g mean?
I'm developing a pattern that validates string if it does not contain more then two matches of #. here is code:
^[^\!|\#|\$|\%|\^|\&|\*|\+][\w]*([\w ]+\#[\w ]*){0,2}$
[^!|\#|\$|\%|\^|\&|*|+]
this is group of not acceptable symbols.
additionally, the pattern should validate string in case if it contains other symbols( - _ , . / ). each symbol should have it's own counter and should not match in any position more than two times.
for example if i have s string like this:
Mcloud dr. #33/#45, some text, some text
it should be valid. but in this case should not:
Mcloud dr. ###33/$#45, ####, ----
What would you suggest ?
Given that you want to match alphanumerics characters and some special symbols ()-_,./ You have to mention them in a character class like this.
Regex: ^(?!.*([(),.#/-])\1)([\w (),.#/-]+)$
Explanation:
(?!.*([(),.#/-])\1) asserts that there shouldn't be more than one character mentioned in character class. This asserts from beginning of string to end.
([\w (),.#/-]+) matches the rest of the string for allowed characters from beginning to end.
Regex101 Demo
I would like to have a regex which matches the string with NO whitespace(s) at the beginning. But the string containing whitespace(s) in the middle CAN match. So far i have tried below
[^-\s][a-zA-Z0-9-_\\s]+$
Debuggex Demo
Above is not allowing whitespace(s) at the beginning, but also not allowing in the middle/end of the string. Please help me.
In your 2nd character class, \\s will match \ and s, and not \s. Thus it doesn't matches a whitespace. You should use just \s there. Also, move the hyphen towards the end, else it will create unintentional range in character class:
^[^-\s][a-zA-Z0-9_\s-]+$
If you plan to match a string of any length (even an empty string) that matches your pattern and does not start with a whitespace, use (?!\s) right after ^:
/^(?!\s)[a-zA-Z0-9_\s-]*$/
^^^^^^
Or, bearing in mind that [A-Za-z0-9_] in JS regex is equal to \w:
/^(?!\s)[\w\s-]*$/
The (?!\s) is a negative lookahead that matches a location in string that is not immediately followed with a whitespace (matched with the \s pattern).
If you want to add more "forbidden" chars at the string start (it looks like you also disallow -) keep using the [\s-] character class in the lookahead:
/^(?![\s-])[\w\s-]*$/
To match at least 1 character, replace * with +:
/^(?![\s-])[\w\s-]+$/
See the regex demo. JS demo:
console.log(/^(?![\s-])[\w\s-]+$/.test("abc def 123 ___ -loc- "));
console.log(/^(?![\s-])[\w\s-]+$/.test(" abc def 123 ___ -loc- "));
You need to use this regex:
^[^-\s][\w\s-]+$
Use start anchor ^
No need to double escape \s
Also important is to use hyphen as the first OR last character in the character class.
\w is same as [a-zA-Z0-9_]
use \S at the beginning
^\S+[a-zA-Z0-9-_\\s]+$
This RegEx will allow neither white-space at the beginning nor at the end of. Your string/word and allows all the special characters.
^[^\s].+[^\s]$
This Regex also works Fine
^[^\s]+(\s+[^\s]+)*$
try this should work
[a-zA-Z0-9_]+.*$
/^[^.\s]/
try this instead it will not allow a user to enter character at first place
^ matches position just before the first character of the string
. matches a single character. Does not matter what character it is, except newline
\s is space
If your field for user name only accept letters and middle of space but not for begining and end
User name: /^[^\s][a-zA-Z\s]+[^\s]$/
If your field for user ID only accept letters,numbers and underscore and no spaces allow
user ID: /^[\w]+$/
If your field for password only accept letters,number and special character no spaces allow
Password: /^[\w##&]+$/
Note: \w content a-zA-Z, number, underscore (_) if you add more character, add you special character after \w.
You can compare with user ID and password field in password field im only add some special character (##&).
India public thoko like 😁
I suggest below regex for this,
^[^\s].*[^\s]$
You can try regex in here
I want to extract a string from another using JavaScript / RegExp.
Here is what I got:
var string = "wp-button wp-image-45 wp-label";
string.match(/(?:(?:.*)?\s+)?(wp-image-([0-9]+))(:?\s(?:.*)?)?/);
// returnes: ["wp-button ", "wp-image-45", "45", undefined]
I just want to have "wp-image-45", so:
(Optional) any character
(Optional) followed by whitespace
(Required) followed by "wp-image-"
(Required) followed by any number
(Optional) followed by whitespacy
(Optional) followed by any character
What is missing here? Is it just some kind of bracketing or more?
I also tried
string.match(/(?:(?:.*)?\s+)?(?=(wp-image-([0-9]+)))(?=(:?\s(?:.*)?)?)/)
Edit: In the end I just want to have the number. But I'd also make this step in between.
Regexps are not required to start matching at the beginning of the string, so your attempts to match whitespace and any character aren't necessary. Also, "any character" includes whitespace (except newlines in certain modes).
This should be all you need:
string.match(/\bwp-image-(\d+)\b/)
This will capture, for example, "wp-image-123" into matching group 0, and "123" into matching group 1.
\b means "word boundary", which ensures that you won't match "abcwp-image-123def". A word boundary is defined as any place where a non-word character is followed by a word character, or vice versa. A word character is consists of a letter, a number or an underscore.
Also, I used \d instead of [0-9] simply out of convenience. They have slightly different meaning (\d also matches characters considered numbers in other languages), but that won't make a difference in your case.
If all of that surrounding stuff is optional and all you want is the number then there's no point to matching for any of that stuff except for that "wp-image-" prefix, just do:
var string = "wp-button wp-image-45 wp-label";
string.match(/wp-image-([0-9]+)/);