Remove $ and R $ in a string

1

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.

    
asked by Nancy Zj 19.10.2017 в 19:02
source

3 answers

1

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.
  • To put 2 options within the pattern, use a | , which indicates alternation .
    The regex /\$|R\$/g would work.
  • However, it is easier to use a quantifier . A ? repeats the previous construction 0 or 1 time. That is, it makes it optional. It's useful for R .

Solution:

texto.replace(/R?\$/g, '');
  • An optional "R" and a "$" sign.
  • The /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">
    
answered by 24.10.2017 / 03:00
source
0

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.

    
answered by 20.10.2017 в 23:36
-1

Try it this way:

$scope.TotalOwed.replace(/(\$)|([R$])/g,'');

Remove: $ and R $

    
answered by 19.10.2017 в 19:46