I have a string like this: "[A] [B] this is my [C] string". How can I take the characters in [] and replace it with others (add and remove [])
-
Is it an array or just a string? – Apr 23 '21 at 17:05
-
that's string I want reform it – DuckFterminal Apr 23 '21 at 17:07
-
Can you explain a bit more? You want to replace A, B and C with what (what is the meaning of 'replace it with others)? Please put a example input and output if possible – Nimna Perera Apr 23 '21 at 17:09
-
Here's a regex that matches what you want to match. [Check this](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – RIdaia Apr 23 '21 at 17:10
-
Let me example: "[A][B] this is my string [C]" ABC in that string will match 123, and I want the output string: "12 this is my string3" – DuckFterminal Apr 23 '21 at 17:17
-
yes, I have also thought of regex. Thank you – DuckFterminal Apr 23 '21 at 17:20
-
Thanks for that info. So you want the order of the match? Like replace the first match with `1`? Or replace the `[A]` match with `1` regardless of order? This is one possible solution `const replaced = s.replaceAll(/\[(.*?)\]/g, (withBrackets, innerText, position) => { /* return something */ })` but it doesn't get the order of the match in the callback. `position` is the index of the character in the string. – Linda Paiste Apr 23 '21 at 17:22
-
I get it from array, example I have string array is ["A", "B", "C"] and number array [1, 2, 3] I just want replace them in order – DuckFterminal Apr 23 '21 at 17:31
-
1I think I've answered correctly? Can you please update your question to explain the requirements so that others can see what you want without reading through the comments. – Linda Paiste Apr 23 '21 at 17:50
2 Answers
There are a lot of ways to approach this. To some extend it depends on what your replacement function is and what information it needs. I believe this first one is what you want. It ignores the contents of the matched targets A
, B
, C
entirely and instead replaces with a pre-defined value based on the order of the brackets.
I am using Array.reduce() and String.replace() with the regex explained in the linked question:
You could remove the parentheses in the regex which denote the capture group since we aren't actually using the captured text.
const replacements = [1, 2, 3];
const input = "[A] [B] this is my [C] string";
const output = replacements.reduce(
(string, replacement) => string.replace(/\[(.*?)\]/, replacement),
input
);
console.log(output);
This next version assumes that the output is based on the text inside the brackets rather than the order or appearance. Therefore [A]
is replaced with 1
regardless of the order that the placeholders appear in the string.
I am using String.replaceAll()
. The callback function gets three arguments which I have tried to give descriptive names. withBrackets
is the full match like "[A]"
, innerText
is the text inside the brackets "A"
, and position
is the index in the string where the match begins.
const input = "[A] [B] this is my [C] string";
// with TS: const replacementFunction = (text: string): string => {
const replacementFunction = (text) => {
return text === "A" ? 1 : text === "B" ? 2 : 3
}
// no TS needed
const replaced = input.replaceAll(/\[(.*?)\]/g, (withBrackets, innerText, position) => {
// do something with the match
return replacementFunction(innerText);
});
console.log(replaced)

- 38,446
- 6
- 64
- 102
If it is a string, then do it this way:
let myString = "[A] [B] this is my [C] string";
// find the left string and replace it with the right
const replaced1 = myString.replace("[C]", "[E]");
console.log(replaced1);