I have this string
This is some text (and text), and this is another text (AND TexT), the completion goes here.
I need in JavaScript -using a regex- to replace all occurrences of the string "text (and text)" with "NewText" (case insensitive). so the final result should be
This is some NewText, and this is another NewText, the completion goes here.
CodePudding user response:
let string = "This is some text (and text), and this is another text (AND TexT), the completion goes here.";
console.log(string.replace( /(\w ) \(.*?\)/gi , "NewText"));
CodePudding user response:
You will just need to escape the parenthesis: here is the regex /text (and text)/ig
\( -> means that the parenthesis should be match as is
i -> case insensitive
g -> global
console.log('This is some text (and text), and this is another text (AND TexT), the completion goes here'.replaceAll(/text \(and text\)/ig, 'NewText'))
CodePudding user response:
const search = 'text (and text)';
const replaceWith = 'NewText';
const result = 'This is some NewText, and this is another NewText, the completion goes here.'.split(search).join(replaceWith);
console.log(result);