Sometimes, we want to fix the string replace method not replacing text containing literal rn strings in JavaScript.
In this article, we’ll look at how to fix the string replace method not replacing text containing literal rn strings in JavaScript.
How to fix the string replace method not replacing text containing literal rn strings in JavaScript?
To fix the string replace method not replacing text containing literal rn strings in JavaScript, we can call replace
with a regex to match all instances of rn and replace them with the characters we want.
For instance, we write:
const value = 'abcrndef'
const newVal = value.replace(/(?:\[rn]|[rn])/g, "");
console.log(newVal)
to call value.replace
to replace all instances of rn with empty strings.
We use the /(?:\[rn]|[rn])/g
regex to find all instances of rn in value
.
As a result, newVal
is "abcdef"
.
Conclusion
To fix the string replace method not replacing text containing literal rn strings in JavaScript, we can call replace
with a regex to match all instances of rn and replace them with the characters we want.