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?

2
  • 3
    Does this answer your question? Replace a line in txt file using JavaScript Commented Jun 21, 2021 at 15:11
  • I tried this 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? Commented Jun 21, 2021 at 15:16

1 Answer 1

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' );
Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain the. regex how it works?
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?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.