-1

I want to replace all the 4 dots(....) in my String with new line (\n) by using regex in the replace method.

orange...apple....banana --> should be pirnted as:

orange
apple
banana

I have tried the following:

get : function(value) {
 value = value.replace(/(...)/g, '\n');
 return value;
}

and

value.replace(/(\r\n|\n)/g, '....') 

--> should retrieve 'orange....apple....banana'

benz
  • 693
  • 1
  • 9
  • 29
  • You can split the input string using `'....'` as delimiter then join the pieces using `'\n'` as follows: `get: function(value) { return value.split('....').join('\n'); }` [`String.split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) produces an array of many small strings that [`Array.join()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) glues together to produce the desired output string. **This method is not recommended for large input strings because it uses a lot of memory**. – axiac Jul 23 '18 at 11:39

2 Answers2

3

You must escape the dot in regular expressions:

value = value.replace(/\.{4}/g, '\n');
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
1

. is a special character in regex, you'll have to escape it

Titus
  • 22,031
  • 1
  • 23
  • 33