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
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:
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) ?(.*)$
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 an answer here:
Parenthesis not being replaced in regex
(1 answer)
Closed 4 years ago.
I'd like to remove all occurences of "(" and ")" in a string but the following replace line is throwing up a 'group not terminated' error.
str = "1+((x*(2*3))+10)";
console.log(str.replace(//(/gi,"");
How should I do this?
A '(' character has special meaning in RegEx (start of Group), you must escape the parentese like this:
\(
The same for an end parentese. Alternatively you can use a character Group like this:
[()]+
That will select any character in the Group (in this case parenteses) on or more times.
Try replacing a character class containing both opening and closing parentheses:
var str = "1+((x*(2*3))+10)";
console.log(str.replace(/[()]+/gi,""));
1+x*2*3+10
But, it is not clear why you would want to do this, because most likely removing all parentheses would change the value of the expression.
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