0

I'm trying to replace multiple strings in a text file with one string using javascript

I want those two strings "user" and "password" must be replaced with 'replace the string' Anyone help me here?

Gow_code
  • 3
  • 3
  • 3
    Does this answer your question? [Replace a line in txt file using JavaScript](https://stackoverflow.com/questions/53446570/replace-a-line-in-txt-file-using-javascript) –  Jun 21 '21 at 15:11
  • I tried this [Replace a line in txt file using JavaScript](https://stackoverflow.com/questions/53446570/replace-a-line-in-txt-file-using-javascript) Replace a line in txt file using JavaScript.but it is only replacing only specific string not entire line. for example. I wanted to replace entire line contains "user":"1232", But it is replacing like this "replace the string" : "1232" not entire line.Anyh help here? – Gow_code Jun 21 '21 at 15:16

1 Answers1

0

to add to the previous replies, you can use capture groups to back reference parts of the matched text i.e.

if you search for "user"

let newText = yourText.replace(/^((\"user\")(:".*")(.*))$/gm, '"removed"$3$4' );

if you search for "1232"

let newText = yourText.replace(/^((\".*\")(:"1232")(.*))$/gm, '"removed"$3$4' );
ko0kiE
  • 16
  • 1
  • can you please explain the. regex how it works? – Gow_code Jun 21 '21 at 15:51
  • var formatted = data.replace(/^((\"user\")(:".*")(.*))$/gm, 'This new line replaces the old line'); If i try the above .it will replace entire line "user":"1232" right? – Gow_code Jun 21 '21 at 15:53