I try to eliminate $
and R$
of a string.
What I've tried is
$scope.TotalOwed.replace(/\$\R$/g, '');
but it did not work for me.
I try to eliminate $
and R$
of a string.
What I've tried is
$scope.TotalOwed.replace(/\$\R$/g, '');
but it did not work for me.
Fixes:
$
is a special character in regex (which coincides with the end of the text). To match the literal character there is escape it as \$
. R
is not a special character. There is no need to escape. Any group of literal characters matches that text. |
, which indicates alternation . /\$|R\$/g
would work. ?
repeats the previous construction 0 or 1 time. That is, it makes it optional. It's useful for R
. Solution:
texto.replace(/R?\$/g, '');
"R"
and a "$"
sign. /g
switch causes it to replace all occurrences, and not just the first one. Demo:
var texto = document.getElementById('texto'),
resultado = document.getElementById('resultado');
function eliminarMoneda(){
resultado.innerText = texto.value.replace(/R?\$/g, '');
}
eliminarMoneda();
texto.addEventListener('input', eliminarMoneda);
<input type="text" id="texto" style="width:100%"
value="Reemplazamos en $123 y R$456">
<pre id="resultado">
You could do this:
$scope.TotalOwed.replace(/(\$|\R\$)/g, '');
And if you want to save the value of $scope.TotalOwed
without $
or R$
then just save it in a variable:
var sin_$_ni_R$ = $scope.TotalOwed.replace(/(\$|\R\$)/g, '');
Or:
$scope.TotalOwed = $scope.TotalOwed.replace(/(\$|\R\$)/g, '');
I hope and serve you.
Try it this way:
$scope.TotalOwed.replace(/(\$)|([R$])/g,'');
Remove: $ and R $