3

I'm trying to find and replace a word in a string

Example:

let string = 
`
Title: Hello World
Authors: Michael Dan
`

I need to find the Hellow World and replace with whatever I want, here is my attempt:

const replace = string.match(new RegExp("Title:" + "(.*)" + "Authors:")).replace("Test")
4
  • When you replace(), you do not need to match(). string.replace(/(Title:).*(?=\nAuthors:)/gs, '$1Test') can be used here. Commented Dec 10, 2021 at 10:18
  • @WiktorStribiżew problem, it doesn't even find the match Commented Dec 10, 2021 at 10:19
  • @WiktorStribiżew could you please elaborate on that? Sorry, don't quite catch it. Commented Dec 10, 2021 at 10:24
  • Ok, I see I might be wrong with the s flag. Anyway, you need to keep the first and last words. Commented Dec 10, 2021 at 10:27

4 Answers 4

4

When you replace some text, it is not necessary to run String#match or RegExp#exec explicitly, String#replace does it under the hood.

You can use

let string = "\nTitle: Hello World\nAuthors: Michael Dan\n"
console.log(string.replace(/(Title:).*(?=\nAuthors:)/g, '$1 Test'));

The pattern matches

  • (Title:) - Group 1: Title: fixed string
  • .* - the rest of the line, any zero or more chars other than line break chars, CR and LF (we need to consume this text in order to remove it)
  • (?=\nAuthors:) - a positive lookahead that matches a location that is immediately followed with an LF char and Authors: string.

See the regex demo.

If there can be a CRLF line ending in your string, you will need to replace (?=\nAuthors:) with (?=\r?\nAuthors:) or (?=(?:\r\n?|\n)Authors:).

Sign up to request clarification or add additional context in comments.

Comments

0

You might be better off converting to an object first and then just defining the title property:

let string = 
`
Title: Hello World
Authors: Michael Dan
`

const stringLines = string.split('\n');
let stringAsObject = {};

stringLines.forEach(
  (line) => {
    if (line.includes(':')) {
      stringAsObject[line.split(':')[0]] = line.split(':')[1];
    }
  }
);

stringAsObject.Title = 'NewValue';

2 Comments

I don't have access to the Hello World. I need to find it first.
In that case, if you know that your properties are always separated by newlines and : characters you might be better off converting to a keyed object and then replacing your title
0

You can use replace method like that:

string.replace("Hello World", "Test");

3 Comments

I don't have access to the Hello World I need to find it first.
I don't get it, it works: jsfiddle.net/5gdbn6sf/2
I'm saying I can't hardcode Hello world. I need to find it first and then replace it. This text is always different.
0

I can achieve this without regex. All you need is knowing the index of the string that you need to find.

var original = `
Title: Hello World
Authors: Michael Dan
`;

var stringToFind = "Hello World";
var indexOf = original.indexOf(stringToFind);
original = original.replace(original.substring(indexOf, indexOf + stringToFind.length), "Hey Universe!");

console.log(original)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.