-3

i have a string where there is no space in between and there is a set of characters which keeps on repeating in the same string, but i want to replace those set of characters with \n. here is what i have tried but i'm not able to see anything

here is the string

lorem%20ipsum%20skdjajsadsa%0D%0Askdjsadkasjdkjasdsjds%0D%0Aadasdkjsadkjsad%0D%0Aaki7yuj%0

in the string there are 3 occurrences of %0D%0A, i want to replace those with \n, how can i do this

here is what i have tried.

str = "lorem%20ipsum%20skdjajsadsa%0D%0Askdjsadkasjdkjasdsjds%0D%0Aadasdkjsadkjsad%0D%0Aaki7yuj%0"

console.log(
  str.replace("%0D%0A", "\n")
);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
CJAY
  • 6,989
  • 18
  • 64
  • 106
  • 1. You need to save the result, i.e. `str = str.replace("....");` 2. use the [`g` global modifier](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) 3. console.log the result, the \n is just a space if you show it in an html container – mplungjan Feb 04 '19 at 16:39

2 Answers2

0

You can use RegEx with the global flag g to remove all occurrences:

var str = "lorem%20ipsum%20skdjajsadsa%0D%0Askdjsadkasjdkjasdsjds%0D%0Aadasdkjsadkjsad%0D%0Aaki7yuj%0"

str = str.replace(/%0D%0A/g, "\n");
console.log(str);
Mamun
  • 66,969
  • 9
  • 47
  • 59
-1

Try

let str = "lorem%20ipsum%20skdjajsadsa%0D%0Askdjsadkasjdkjasdsjds%0D%0Aadasdkjsadkjsad%0D%0Aaki7yuj%0"

let r= str.replace(/(%0D%0A)/g,"\n");

console.log(r);
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345