Regex not allow empty spaces at the end [duplicate] - javascript

This question already has answers here:
RegEx for no whitespace at the beginning and end
(18 answers)
Closed 2 years ago.
My target is to improve my regex. Regex need to everything except some special charaters, not to allow empty spaces at start, and not to allow empty spaces at the end.
^(?!\s*$)[^-\s][^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
Example:
word valid
word [space] invalid
[space] word invalid
word w valid
My regex did everything except empty space at the end. How to add this condition to forbit empty spaces at the end of regex?

You may add another negative lookahead to disallow space at the end:
^(?!\s*$)(?![^]*\s$)(?![-\s])[^`=~!##$%^&*()[\]\/\\{}"|<>?]{3,100}$
(?![^]*\s$) is negative lookahead to assert that your regex won't allow a space at the end.
RegEx Demo

Related

What is the regex that allows alphanumeric and '-' special character that also should be in between the text not at the begining or ending [duplicate]

This question already has answers here:
Regex to match '-' delimited alphanumeric words
(5 answers)
Closed 8 months ago.
All the special character except hyphen are not allowed.
Other conditions:
-xnnw729 //not allowed
nsj28w- // not allowed
aks82-z2s0j // allowed
Some notes about your answer:
Using \w also matches \d and _
For a match only you don't need all the capture groups
If you want to validate the whole line, you can append $ to assert the end of the line
Using a plus sign in the character class [\w+\d+_] matches a + character and is the same as [\w+]
You can simplify your pattern to:
^\w+(?:-\w+)*$
Regex demo
The one I was looking for is
^([\w+\d+_]+)((-)([\w+\d+_]+))*

Optional capture group till end of line [duplicate]

This question already has answers here:
Match whitespace but not newlines
(7 answers)
Closed 2 years ago.
Given the following strings
I have the following regex
^\/\/\s(prevent\s?eval\s|eval\s?prevent)\s?([^\n]*)$
What I am trying to do is match the following:
// preventeval
hello world
[preventEval]
// preventeval meow
hello world
[preventEval, meow]
https://regexr.com/529p7
\s matches any whitespace character, and a newline character is a whitespace character, so it gets matched by it (and then the whole next line is matched by ([^\n]*)).
Optionally match a plain space instead (and remove the \s from the end of prevent\s?eval\s in the first alternation).
You can also use .* instead of [^\n]*, you probably wouldn't want to match a linefeed character anyway:
^\/\/\s(prevent\s?eval|eval\s?prevent) ?(.*)$
It sounds likely that you don't want any of the \ss to match newlines in which case, replace them all with plain spaces:
^\/\/ (prevent ?eval|eval ?prevent) ?(.*)$

Remove line spaces [duplicate]

This question already has answers here:
JavaScript: how to use a regular expression to remove blank lines from a string?
(5 answers)
Closed 2 years ago.
How can I remove the line spaces but without remove the structure of the next string.
P0:::inicial
P1:::pregunta
P2:::pregunta
P3:::pregunta
R1:::respuesta
R2:::respuesta
R3:::respuesta
R4:::respuesta
R5:::respuesta
R6:::respuesta
R7:::respuesta
R8:::respuesta
R9:::respuesta
C1:::cotizacion
C2:::cotizacion
C3:::cotizacion
A1:::agregar
A2:::agregar
<------ I want delete this spaces.
C4:::cotizacion
I try using regex but I have problems when the regex and replace remove all the spaces on my string but I only want delete the spaces without information.
In try some like this:
replace(/[\r\n]+/g, " ");
You may use this regex with anchor and MULTILINE flag:
.replace(/^[ \t]*[\r\n]+/gm, "")
RegEx Demo
MULTILINE or m mode makes ^ match start of each line.
^: matches start of a line
[ \t]*: Match 0 or more spaces or tabs
[\r\n]+: Match 1+ line break characters \r or \n

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.

Javascript Regex for custom validation [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I need regex
1) [A-Z][a-z][0-9] Must include uppercase & lowercase letters, numbers & special characters (except + and -).
2) Not more than 2 identical characters in a sequence (e.g., AAxx1224!# or Password#123 or Google#12 is not acceptable).
I have tried this but dont know how to check 2 identical characters.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^%*()!&=]).*$
You may add an additional (?!.*(.)\1) lookahead check to disallow consecutive characters and replace .* at the end with [^_+]* (or [^-+]* if you meant hyphens) to match any chars but _ (or -) and +:
^(?!.*(.)\1)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^*()!&=])[^_+]*$
^^^^^^^^^^ ^^^^^
The (?!.*(.)\1) lookahead matches any 0+ chars other than line breaks chars and then captures these chars one by one and tries to match the identical char immediately after them (with the \1 backreference). If the pattern is found, the whole match is failed.
Note that [^_+] may also match line breaks, but I guess it is not the problem here. Anyway, you can add \n\r there to avoid matching them, too.
See the regex demo

Categories