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) ?(.*)$
Related
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+_]+))*
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
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
This question already has answers here:
How do I match any character across multiple lines in a regular expression?
(26 answers)
Closed 4 years ago.
I have some confusion in Regex so I need help.my question is I am using the following Regex to prevent string should not start with some character and should not contain angular bracket.this regex also preventing next line as well so can u help me to modify it according to my need.
^(?![#=+*-])(?!.*[<>]).*$
Thanks
working example-->https://regex101.com/r/5GZQl7/1
The problem with your regex is that . does not match line endings, so as soon as you put a new line in there, the regex does not match.
Ideally, we want it to match everything, including line endings. What syntax can match everything? One way to do this is to use complementing character sets. \s matches all the whitespace, \S matches all the non-whitespace, so [\s\S] will match everything!
Replace all your .s with [\s\S]!
Demo
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