How to replace by using regular expression in JavaScript?
Input:
The is currency \2,501 and \245,870.
Output:
The is currency ¥2,501 and ¥245,870.
How to replace by using regular expression in JavaScript?
Input:
The is currency \2,501 and \245,870.
Output:
The is currency ¥2,501 and ¥245,870.
The backslash tries to escape the following character. For instance, \n is a newline character.
In your case you can not find backslash in your string because you tries to escape the following "2" character. In this case you have to write two backslashes to escape one backslash like follows:
var str = 'The is currency \\2,501 and \\245,870.';
console.log(str.replace(/\\/g, '¥')); //you get it here like ¥ because HTML is already loaded
//or like follows:
var yen = document.querySelector('div').innerHTML;
console.log(str.replace(/\\/g, yen));
//or direct like follows:
console.log(str.replace(/\\/g, '¥'));
<div>¥</div>