Regex not validating end of string - javascript

Consider the following scenario (Javascript code):
regex = new RegExp((/([\d,.]+)[ $]/));
value = "2.879"
The regexp doesn't match value, but it matches (value+" ") therefore i think that the $ is not matched? Why is that?
Shouldn't the $ validate the end of string?

Special characters like $ don't have the same meaning inside a character class. In a character class they're just characters, so [ $] will match either the space character or the $ character. It won't match the end of a string.
If you want to match either a space character or the end of the string, you should use alternation, i.e. ( |$).

Related

What would be Regular Expressions (Regex) to check string for start with $ and does not end with special character

In my string, I only allow the following 5 special characters #_.-$
RegExp = /^[a-zA-Z0-9#$_.\\-]+$/
How do I check if a string is starting with character $
also, another looking for RegExp for a string to not end with a special character
my_regexp = /^\$[a-zA-Z0-9#$_.\\-]*[a-zA-Z0-9]$/
This omits the special characters from the character set of the last character.

what is javascript regex to validate an array of string value

I want to validate an array of a single element of type string using regex. The array should not be empty and should not start with any special symbol except # and the string can include only numbers and alphabets.
what i tried
[a-zA-z0-9s!##$%^&*()_/\+=.,~`]+
you can try
regex = /^#?[a-zA-Z0-9]+/gm;
Try this:
regex = /^#?[a-z0-9]+/gi;
Breakdown:
/^ - Match the start of the string
#? - Match an optional #
[a-z0-9] - Character set including the lowercase alphabet, and all ten digits
+ - Match one or more of the preceding character set
/gi - Global and case-insensitive flags

Removing non-alphanumeric text with String.prototype.replace

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?

Write a regex for usernames

I want a Regex for my mongoose schema to test if a username contains only letters, numbers and underscore, dash or dot. What I got so far is
/[a-zA-Z0-9-_.]/
but somehow it lets pass everything.
Your regex is set to match a string if it contains ANY of the contained characters, but it doesn't make sure that the string is composed entirely of those characters.
For example, /[a-zA-Z0-9-_.]/.test("a&") returns true, because the string contains the letter a, regardless of the fact that it also includes &.
To make sure all characters are one of your desired characters, use a regex that matches the beginning of the string ^, then your desired characters followed by a quantifier + (a plus means one or more of the previous set, a * would mean zero or more), then end of string $. So:
const reg = /^[a-zA-Z0-9-_.]+$/
console.log(reg.test("")) // false
console.log(reg.test("I-am_valid.")) // true
console.log(reg.test("I-am_not&")) // false
Try like this with start(^) and end($),
^[a-zA-Z0-9-_.]+$
See demo : https://regex101.com/r/6v0nNT/3
/^([a-zA-Z0-9]|[-_\.])*$/
This regex should work.
^ matches at the beginning of the string. $ matches at the end of the string. This means it checks for the entire string.
The * allows it to match any number of characters or sequences of characters. This is required to match the entire password.
Now the parentheses are required for this as there is a | (or) used here. The first stretch was something you already included, and it is for capital/lowercase letters, and numbers. The second area of brackets are used for the other characters. The . must be escaped with a backslash, as it is a reserved character in regex, used for denoting that something can be any character.

Regex: string up to 20char long, without specific characters

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

Categories