Replace multiple strings between two indexes with in strings

abinas patra

I have a string. I want to replace substrings with in string. For each substring I have the starting and ending index. Using regex is out of scope.

So from let str = "I want chicken pizza and cheese pizza from point restaurant";

The expected result would be like this.

I want TYPE1 TYPE4 and TYPE1 pizza from point restaurant

let str = "I want chicken pizza and cheese pizza from point restaurant";

let roles = [{
    "start": 7,
    "end": 14,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "chicken"
  },
  {
    "start": 25,
    "end": 31,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "cheese"
  },
  {
    "start": 15,
    "end": 20,
    "typeId": "TYPE4",
    "type": "main ingredient",
    "text": "pizza"
  }
];

let styledStr = str;
roles.map(r => {
  const { start, end, typeId, text } = r;
  let strArr = str.split('');
  let removeStr = strArr.splice(start, end, typeId);
  styledStr = strArr.join('');
  console.log(styledStr);
});

mplungjan
  1. Do not use a map if you do not need a
  2. No need to split unless you MUST use splice

Method using start and end - note I sort the roles so we start at the highest position

let str = "I want chicken pizza and cheese pizza from point restaurant";

let roles = [{
    "start": 7,
    "end": 14,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "chicken"
  },
  {
    "start": 25,
    "end": 31,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "cheese"
  },
  {
    "start": 15,
    "end": 20,
    "typeId": "TYPE4",
    "type": "main ingredient",
    "text": "pizza"
  }
];

let styledStr = str;
roles.sort((a,b)=>b.start-a.start)
roles.forEach(r => {
  const { start, end, typeId, text } = r;
  // Using substring because it is more readable and saves a split
  styledStr = styledStr.substring(0,start) + typeId + styledStr.substring(end)
  styledStr.slice(start,end,typeId)
});
console.log(styledStr)

Method using text replace

let str = "I want chicken pizza and cheese pizza from point restaurant";

let roles = [{
    "start": 7,
    "end": 14,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "chicken"
  },
  {
    "start": 25,
    "end": 31,
    "typeId": "TYPE1",
    "type": "toppings",
    "text": "cheese"
  },
  {
    "start": 15,
    "end": 20,
    "typeId": "TYPE4",
    "type": "main ingredient",
    "text": "pizza"
  }
];

let styledStr = str;
roles.forEach(r => {
  const { start, end, typeId, text } = r;
  console.log(start,end,text)
  styledStr = styledStr.replace(text,typeId)
});
console.log(styledStr)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace multiple occurrences between two strings

joining strings iteratively between two indexes python

Replace a String between two Strings

PHP replace first empty space between two strings (multiple occurrences)

Doing a find-and-replace between two strings, across multiple lines

Replace segment of text, across multiple lines between two strings - regex

replace strings with another string between two $ signs

Replace multiline text between two strings

Replace() before html() and add "&" between two strings

sed to Find and Replace chacters between two strings

How replace chars between two strings

MYSQL Replace string between two known strings

Replace instances of certain string in between two strings

Replace all occurrences of a character between two strings

How to replace string between two strings in javascript?

Replace strings in pandas cell using regex, between two specific strings

Replace multiple strings between characters in JavaScript

Notepad++ replace text between two strings spawning multiple lines and retain some part of the string in between

Splitting two strings with matching indexes into multiple rows in SAS

How to get multiple strings between two other strings

PowerShell Regex: Capturing strings between two strings that is on multiple lines

Extract multiple Strings between two String in PHP

Replace multiple strings at once

Check for multiple replace strings

replace multiple strings in python

Replace string with multiple strings

replace strings in multiple lines

Replace multiple strings or texts

Replace multiple strings with multiple other strings