Regex in Javascript using OR [duplicate] - javascript

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.

Related

How to replace brackets in string using regex in node js? [duplicate]

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.

JS: check if a single character is NOT a whitespace [duplicate]

This question already has answers here:
regex to match a single character that is anything but a space
(3 answers)
Closed 4 years ago.
How to check in JavaScript if the specified character is not a whitespace using regex only? Right now I am doing something like the code below with negation ! but I would like to avoid mixing of two things to avoid confusions.
if (!/\s/.test(character))
console.log('this is not a whitespace');
if (/\S/.test(character))
console.log('this is not a whitespace');
Use the negated set notation in the regex
/[^\s]/
That will match everything that isnt a whitespace.

javascript regex find words contains (at) in text [duplicate]

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,}

Use regex backreference inside character except class [^] [duplicate]

This question already has answers here:
Negating a backreference in Regular Expressions
(6 answers)
Closed 4 years ago.
Lets take:
stringi = 'xnxx xnnx xnnxn'
My regex is: (n)[^n]
I want to make my regex a little more dynamic like that:
(n)[^\1] -\1 beeing the capt. grp 1
My desired result would be that:
(n)[^\1] would be equal (n)[^n]
(x)[^\1] would be equal (x)[^x]
How can I not match a NOT-\1 character?
using a negative lookahead, the . is to match any character as n length is one
(n)(?!\1).

Replace pattern within string with space [duplicate]

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, '');

Categories