1

I have a text string i want to replace it by another text using regexp

Means each word replaced with a specific word

input= Jounal Page Year DOI Roche-Link

my expected output

output = Publication Title Pagination Publication Date Digital Object Identifier Roche Link

Journal replace by Publication Title

Page replace by Pagination

Year replace by Publication Date

DOI replace byDigital Object Identifier

Roche-Link replace by Roche Link

  • my regex=\b(Journal|Year|Page|DOI|Roche-Link)\b

my regexp Detect all the specific words but I didn't find a solution using $ to replace each word the with the specific word

output = input.replace(/\b(Journal|Year|Page|DOI|Roche-Link)\b/g, (Publication Title)+$1, (Pagination)+$2,(Publication Date)+$3,(Digital Object Identifier)+$4,(Roche Link)+$5)
MokiNex
  • 857
  • 1
  • 8
  • 21

1 Answers1

1

You could do something like this:

var dict = {
  "Journal": "Publication Title",
  "Page":  "Pagination",
  "Year":  "Publication Date",
  "DOI": "Digital Object Identifier",
  "Roche-Link":  "Roche Link"
};

var regExp = new RegExp("\\b(" + Object.keys(dict).join("|") + ")\\b", "g");

var translated = "Journal Page Year DOI Roche-Link".replace(regExp, function(match) {
  return dict[match];
});

console.log(translated);
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28