0

I have a stupid Problem within Vue: I'm getting an Object which looks like this:

`{ "description": "this ist our List.\n Our Service includes:\n-1 something\n-2 something else\n and another thing\n"}`

how can i filter out the "\n" and the "-1,-2..." and replace them with htm tags, like
?

I already tried a function, which gets a value like:

returnList: function(value){
  value.replace("\n","<br>")
  return value`

But it doesnt work for all \n's

Ele
  • 33,468
  • 7
  • 37
  • 75
Petziferum
  • 209
  • 1
  • 2
  • 12

3 Answers3

3

Firstly, you should know that String.prototype.replace function returns the result of the replacement.

let obj = {
  "description": "this ist our List.\n Our Service includes:\n-1 something\n-2 something else\n and another thing\n"
};

console.log(obj.description.replace(/\n-[0-9]+/g, "<br>"))

Another regex approach: /\n(-\d)?/g

Ele
  • 33,468
  • 7
  • 37
  • 75
1

You need to use regex. Like that: value.replace(/\/n/g,"<br>")

Gawel1908
  • 138
  • 1
  • 9
-1

You should change this:

returnList: function(value){
  value.replace("\n","<br>")
  return value`
}

to this:

returnList: function(value) {
  value = value.replace("\n","<br>")
  return value;
}

Becuase .replace does not change value itself, but return a new value with the changes.

But, will only change the first value anyway. So you could use this solution value.split("\n").join("<br>").

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130