0

I have a Javascript string that ends in \n and I want to remove the last instance of \n through only regex. So I wrote this query /..$/ which targets it. However, when I run string1.replace(/..$/, "") the .replace still returns the original string and does not replace the last instance of \n with an empty string.

How can I use regex to remove those last two characters?

I've tried this

let string1 = '0,1,2,3,45\n10,11,12,13,14\n20,21,22,23,24\n30,31,32,33,34\n'

string1.replace(/..$/, "") 

=> '0,1,2,3,45\n10,11,12,13,14\n20,21,22,23,24\n30,31,32,33,34\n'
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Dog
  • 2,726
  • 7
  • 29
  • 66

1 Answers1

2

This worked in the console for me:

string1.replace(/\n$/,"");
Collin G.
  • 466
  • 2
  • 9
  • 1
    That works. Rubular must recognize \n as characters so ..$ works there, but in console they aren't recognized and need to be specifically identified as \n. Thank you. – Dog Dec 30 '17 at 20:08