2

if someone could give me the correct replace for : |TEST_1608224413246_Jean_Lol| replace both | with nothing

I have already tested messagenew.replace("|", ""); but it only replace the first |

abc123
  • 17,855
  • 7
  • 52
  • 82
Gamepigeek
  • 21
  • 4
  • 3
    Does this answer your question? [How to replace all occurrences of a string?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string) – Sarabadu Dec 17 '20 at 19:11

1 Answers1

4

Using string.replace

// define let to store the test string
let s = '|TEST_1608224413246_Jean_Lol|'

// use string.replace to use regex to find and replace the | character
// | is a special character in regex so we must use \| to escape it
// using g to globally replace this means find all
// second parameter is what to replace it with empty string
s = s.replace(/\|/g, '')

// log the result as this is a demo
console.log(s)

Using string.replaceAll

// define let to store the test string
let s = '|TEST_1608224413246_Jean_Lol|'

// use string.replaceAll to find and replace the | character
s = s.replaceAll('|', '')

// log the result as this is a demo
console.log(s)

Note

This would replace any and all | not just 2, if specific number 2 is important here the regex could be updated to locate the beginning and end of string and replace them only from there.

abc123
  • 17,855
  • 7
  • 52
  • 82