-4

Currently I am getting strings that look like this:

I like {{Pizza|Pasta}} for breakfast.

I want to manipulate strings in JS so that they only contain the part after the '|' so that the sentence would be

I like Pasta for breakfast.

So far this piece of code has brought me a fair end.

str.replace(/\{\{.*?\|.*?\}\}/g, "");

But if I have a string like:

I eat {{bread}} and {{cereal|yoghurt}} for breakfast.

The results is:

I eat yoghurt for breakfast.

But I want:

I eat bread and yoghurt for breakfast.

So far I have tried this with regex. But it doesn't seem to replace anything.

str.replace(/\{\{.*((?!]).).*?\|(.*?)\}\}/g, "");
7
  • What have you tried, and what exactly is the problem with it? Commented Jun 1, 2020 at 18:29
  • I have tried splitting at the '|' the string and deleting the first part, but then I have the problem that I delete things that I don't want to delete, in the example the 'I like' part Commented Jun 1, 2020 at 18:37
  • So give a minimal reproducible example showing the problem. Commented Jun 1, 2020 at 18:37
  • Does this answer your question? Remove part of string between two indexOf in javascript Commented Jun 1, 2020 at 18:38
  • @jonrsharpe Did you not understand my question? Commented Jun 2, 2020 at 14:00

2 Answers 2

0

This is the perfect use case for a regular expression. You could use one to find all occurances of {{...|...}} and replace them with the part between | and }}

let string = "I like {{Pizza|Pasta}} for breakfast and {{Pasta|Pizza}} for lunch."

console.log(string.replace(/\{\{.*?\|(.*?)\}\}/g, "$1"));

And there they are, multiple placeholders :D

thanks for the help but I unfortunately have another problem. If I have the string "I eat {{bread}} and {{yoghurt|cereal}} for breakfast" it will results in "I eat cereal for breakfast, but I don't want the bread part to disappear.

It does that, because here you have changed the pattern for the placeholder. From exactly {{...|...}} (what the regex above covers) to a |-sperated list of entries inside of the {{...}}.

There are basically two ways to go about this.

Change the regex to reflect that that list:

let string = "I eat {{bread}} and {{yoghurt|cereal}} for breakfast"

console.log(string.replace(/{{(?:.*?\|)*?([^|]*?)}}/g, "$1"));

Or the we go a bit simpler with the regex and let a function deal with its content:

let string = "I eat {{bread}} and {{yoghurt|cereal}} for breakfast"

console.log(string.replace(
  // the placeholder is simply `{{....}}`
  /{{(.*?)}}/g, 
  // the function takes the content between `{{` and `}}` 
  // splits it by `|` and returns the last item
  (match, content) => content.split("|").pop()
));

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

6 Comments

This works perfectly for me! Thank you.
You dont need to escape curely braces, nether you need to add the interogation mark, it can be just console.log(string.replace(/{{.*\|(.*)}}/g, "$1"));
@SaymoinSam I'm surprised about the curly braces because they are used for intervals /\d{3,5}/ and therefore a syntax element imo. But you seem to be right. "... nether you need to add the interogation mark" nor do you need the g flag. Unless you want to support multiple matches. And then you get in trouble with greedy .* selectors. Without the interogation marks, the regex will match everything from the first {{ to the last }} as a single match.
Ah I didnt see the global modifier g
No problem. It wasn't a requirement by OP, but from personal experience, it won't take long untill it will become a requirement. And it was simply too simple to include.
|
-1

Try this one:

var text = '{{Pizza|Pasta}}';
var replaced = text.replace('{{','').replace('}}','').split('|')[1];
var final = 'I like '+replaced+' for breakfast';
console.log(final);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.