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.
Related
This question already has answers here:
RegEx - Get All Characters After Last Slash in URL
(8 answers)
Closed 2 years ago.
I currently have a url path like so:
var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
I wish to delete/remove all the string up to the last / slash.
If I wanted to replace the last slash I'd get it like this:
console.log(str.replace(/\/(?=[^\/]*$)/, '#'));
But I would like to delete everything up to the last slash and I can't figure it out.
In the above example I'd get back
you-do-too
Any ideas?
var str = "http://stackoverflow.com/i-like-upvotes/you-do-too";
console.log(str.replace(/^.*\//, ''));
^ matches the beginning of the string.
.* matches anything.
\/ matches slash.
Since .* is greedy, this will match everything up to the last slash. We delete it by replacing with an empty string.
I know it's not beautiful, but you can do str.split('/').pop() to get the last part of the URL.
No un-humanly readable regex
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:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I've got a bunch of strings to browse and find there all words which contains "(at)" characters and then gather them in the array.
Sometimes is a replacement of "#" sign. So let's say my goal would be to find something like this: "account(at)example.com".
I tried this code:
let gathering = myString.match(/(^|\.\s+)((at)[^.]*\.)/g;);
but id does not work. How can I do it?
I found a regex for finding email addresses in text:
/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
I think about something similar but unfortunately I can't just replace # with (at) here.
var longString = "abc(at).com xyzat.com";
var regex = RegExp("[(]at[)]");
var wordList = longString.split(" ").filter((elem, index)=>{
return regex.test(elem);
})
This way you will get all the word in an array that contain "at" in the provided string.
You could use \S+ to match not a whitespace character one or more times and escape the \( and \):
\S+\(at\)\S+\.\w{2,}
This question already has answers here:
How to match multiple occurrences of a substring
(3 answers)
Closed 6 years ago.
I'm trying to write a regex expression to match multiple characters such as , OR . OR : OR ( OR )
I have this
removePunctuation = /\.$|\,$|\:|\(|\)/;
word = rawList[j].replace(removePunctuation,"")
I just want to remove periods & commas at the end of the sentence but all instances of ( ) :
What am I doing wrong?
You need to add the "g" qualifier if you want to remove all the punctuation.
removePunctuation = /\.$|\,$|\:|\(|\)/g;
I'd go with something like this:
/[.,]+$|[:()]+/g
It'll match periods and commas at the end of a sentence, and brackets and colons everywhere.
This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 7 years ago.
I am getting long string with multiple occurances of pattern './.'. The string has dates as well in a format of dd.mm.yyyy.
First I tried with javascript replace method as:
str.replace('./.', ''). But it replaced only first occurance of './.'
Then I tried another regex which replaces special characters but it didn't work as it replaced '.' within dates as well.
How do I replace multiple occurances of a pattern './.' without affecting any other characters of a string ?
. is a special character in a regexp, it matches any character, you have to escape it.
str.replace(/\.\/\./g, '');
Use this simple pattern:
/\.\/\./g
to find all "./." strings in your text.
Try it :
str.replace(/\.\/\./g, '');
Escape . and d \
Add a g for global
Like this
str = str.replace(/\./\./g, '');