Click here to Skip to main content
15,792,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone!, My question is quite simple But a bit confusing:

How do I replace a specific 100% match word in a string in Javascript/NodeJS?

For an example, Let's say I have this string:

var String = "123 Hi my name is ftk [1] > | I like to play [] games [1] , I have 111 games on my PC."

As you can see above, The string has multiple symbols and multiple numbers as well, Now I want to replace every [1] to be empty or replaced by another word, So the output will be:

String = "123 Hi my name is ftk > | I like to play [] games , I have 111 games on my PC."

Notice that neither
has been gone, But only <[1]>, I just want to replace any word that matches my word 100% if that makes sense.
Thanks soo much in advance!

What I have tried:

I have tried 2 ways:

1- Using .relpace():
var String2 = String.replace("[1]", "");

But this one removes everything included in the string, It'll remove 1, [, and ], And I only want to remove every word that looks exactly like the given one

2- Using global variable to remove all repeated one, Which is what I want, But same thing, It behaves like 1.

var String2 = String.replace(/[1]/g, "");
//and tried using quotes with it and it didn't remove anything
var String2 = String.replace(/"[1]"/g, "");
Updated 10-Jan-23 4:16am

1 solution

Here you go.
Try :
.replace(/\[1|\]/g, '');

I see the result like the following when I use that:
"123 Hi my name is ftk  > | I like to play [ games  , I have 111 games on my PC."
Share this answer
ftk789 10-Jan-23 9:06am    
Oh wow that totally works!, But what does it mean? If you don't mind explaining, I want to learn it to know it works, Thank you soo much for your response though!
raddevus 10-Jan-23 9:17am    
The addition of the / means replace all of the (left bracket, followed by 1) [1
Then there is the | (or) (right bracket) \]

So in natural language it would be something like:
Replace All left bracket followed by 1
or any right bracket ] with an empty char
ftk789 10-Jan-23 9:21am    
That really makes sense now, But what if I have an array and I want to use it in there? Would I just call the array? Or would I have assign multiple If's for each array? My array would be: [1], [2], [3], [4], Etc..., It'll all be numbers, Is that possible? Or no?
raddevus 10-Jan-23 9:39am    
Are you looking for something like:

let allValues = ["first","second","third","fourth"]; => console.log(`I like ${val}`))
I like first
I like second
I like third
I like fourth
Or if you're trying to replace multiple values in the one string with multiple values from an array, it becomes a bit more imperative-style programming -- step-by-step.
In that case you'd have to 1) get the value from the string 1,2,3 etc then get the target value from the array, then replace the value in the original string with that value.
ftk789 10-Jan-23 9:55am    
I like the 2nd option, I do have them in an array, And I have the whole string ready, Will I have to use a for loop? or a while condition?, Here's where it get's hard on my head ,Cuz I can use both, But I want the best.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900