Doubt with Javascript Replace

4

My situation : I have a masked input like this: 0,0000 % . However, to be able to validate as a wish, I need to use a replace , changing the comma by point, removing the % and then treating the value this way: 0.0000 .

I know I can use something like numero.replace(",",".") but this will only change the comma by the point, and my validation will continue to fail.

Is it possible in a single replace or similar code, change the characters and remove % ?

Although the problem has already been solved, here is my answer to Sergio :

In my validation, I can have numbers only less than or equal to 100. Regarding the format (string or number), I can not say with 100% certainty. I think it will have to be number , as this value will probably be stored in a DB and as a string I think it will not be very viable.

Detail : I do not recommend that I use anything back-end for now. Only front-end . It's more a client side validation not to let the user send bizarre values in the post.

    
asked by anonymous 11.08.2014 / 16:14

3 answers

6

It is possible with regular expression. I am not master of the subject, but this seems to account for:

var re = /^(\d+),(\d+) %$/;
console.log("0,0000 %".replace(re, "$1.$2"));

The above regular expression works like this:

  • ^ : from the beginning of the string
  • (\d+) : there must be one or more digits; the parentheses are to capture what marry this part of the expression
  • , : followed by comma
  • (\d+) : followed by one or more digits (again caught)
  • % : followed by space and percentage sign
  • $ : end of string (that is, nothing after percentage)

In the second part of replace , the two groups captured with parentheses are referenced as $1 and $2 respectively. That is, $1 are the digits before the comma, and $2 are the digits that come after it. We set up a new string with these two terms, and . in the middle instead of , .

Considering its edition, this expression seems insufficient because it allows any number, even if greater than 100 (except negatives, which you did not mention but I am considering invalid). A more precise expression:

/^(\d{1,2}|100),(\d+) %$/

The new part, (\d{1,2}|100) , means that before the comma there must be any two digits, or the value "100" .

    
11.08.2014 / 16:20
2

One more suggestion besides the good response from @bfavaretto .

Since it has relatively small numbers you can use parseFloat to convert to Number. So just change the , to parseFloat to be able to work with the number:

var string = '0,00123%';
var numero = parseFloat(string.replace(',', '.'));
console.log(numero); // 0.00123 
console.log(typeof numero); // number

Example: jsFiddle

    
11.08.2014 / 17:29
0
<script>
    var numero = '0,0000 %';
    alert(numero.split(',').join('.').split('%').join('').trim());
</script>

If you use a split to break the String and join to join, having some character or not as an end, consumes less processing in the browser and is faster than using a regular expression. A trim at the end to remove the empty space.

link

    
17.07.2015 / 22:54