No need for regex, and even if it still needed you would have to use a loop, as it is or while to solve other things.
Create a simple object to map what you want to replace and use String.prototype.replaceAll
with string already resolves:
let expression = "5,5x8÷7";
const expressoes = { ',': '.', 'x': '*', '÷': '/' };
for (const [exp, value] of Object.entries(expressoes)) {
expression = expression.replaceAll(exp, value);
}
console.log(expression);
Of course if the goal is to use this with eval()
, it will probably be something "messed up", although it works there are several problems that can occur due to data entry that you can not fully control, the best would be to create something own or use an existing library that already does it, as https://www.npmjs.com/package/mathjs (must have smaller libs that do only the part you want, this does much more, then edit if you find one that really is simple and efficient).
Version ES5:
Like const [...]
and replaceAll
are more modern, if you want something for old browsers you will need regex yet (you could do with String.prototype.split()
also):
var expression = "5,5x8÷7";
var expressoes = { ',': '.', 'x': '*', '÷': '/' };
for (var exp in expressoes) {
expression = expression.replace(new RegExp(exp, 'g'), expressoes[exp]);
}
console.log(expression);
With split
may even work and look faster, but it will depend a little on the size of the input value, in general it was still 3% faster, which is something tiny:
var expression = "5,5x8÷7";
var expressoes = { ',': '.', 'x': '*', '÷': '/' };
for (var exp in expressoes) {
expression = expression.split(exp).join(expressoes[exp]);
}
console.log(expression);
However the quickest was the suggestion of the other answer, working char
to char
as you can see in the benchmark: https://jsbench.me/usko4noh4h/5, being on average 55% faster than the others, which to treat several expressions will be quite advantageous in a matter of time:
let output = '';
for (const char of expression) {
output += char in expressoes ? expressoes[char] : char;
}
An equivalent for ES5 would be (using for
"normal"):
var expression = "5,5x8÷7";
var expressoes = { ',': '.', 'x': '*', '÷': '/' };
var output = '';
for (var i = 0, j = expression.length; i < j; i++) {
var char = expression[i];
output += char in expressoes ? expressoes[char] : char;
}
console.log(output);
The fastest was with "normal":
It is worth remembering that the
String.prototype.replace
, when used with string in the first argument, it does not replace of all occurrences; only the first one. In this type of situation, I would find it preferable to use theString.prototype.replaceAll
.– Luiz Felipe
@Luizfelipe yes, my fault, missed the "global", adjusted.
– Guilherme Nascimento
Then if you want to do the benchmark with the another answer, I’ll be glad to know.
– Luiz Felipe
@Luizfelipe very likely to use char will be much more efficient than anything, since it operates within its own value, in any engine, but I already add.
– Guilherme Nascimento
I also put an answer - just to serve as an example of what not to do :-) - but I think you don’t even need to put in the benchmark, it’s almost certain that it will be slower... cc @Luizfelipe
– hkotsubo
@hkotsubo and Luiz, put the "ES5 version" of the code (using "normal") and it was the fastest, being at least 50% more efficient than the others.
– Guilherme Nascimento