Javascript string replace with calculations -


Is there a way to solve mathematical expressions in strings in JavaScript? For example, suppose I want to create a string of "Tom's 2 apples," 3 of Lucia's apple. Together they have 5 apples "but I want to be able to substitute in the variable. I can do this with a string replacement:

  string =" Tom has apple, Lucy has Y apple. Together (/ / y /, '3'). Replace (/ / Z /, '5');  

However, it would be better if, instead of having a variable Z, I can use X + Y. Now, I can also replace a string for X + Y and it can Can change with the right value, but it is possible that all the possible string calculations are trying to deal with, which I do I think I'm looking for a way to get it:

  string = "some [x], some [or] some [[x + y ^ 2] / (5 * x)] ";  

and [___] parts can be interpreted as an expression to solve before replacing it back into the string.

Thank you for your help

There is no direct, inherent way (OK, OK Is probably there & nbsp; & mdash; see below ), But if you use the callback feature of the replace function, then there may be a function instead of the replacement string (the return value is the one that has been replaced), you can do this quite easily Can apply from

For example, suppose you use Ruby notation # {xyz} for your placeholders. This code hides through those people:

  var mapping, str; Str = "a # {X} three # {Y} five"; Mapping = {"X": 2, "Y": 4}; Str = str.replace (/ \ # \ {([^ #] +) \} / g, function (match, key) {var results; result = mapping [key]; / * ... processing here * / return result; });  

The resulting string is one 2 three 4 five , because # {X} and # {y} Has been replaced by lookup. You can see the key and see whether it is an expression or not, instead of being raised only requires evaluation. This is evaluation, where your real work comes.

Now, you could with to and eval to get the expression of the expression; Result = Mapping [key]; Replace the above line:

  with (mapping) {result = eval (key); }  

If you feed the string "a # {X} three # {Y} five # {X + Y * 2}" , the result One 2 three 4 5 10 & nbsp; & Mdash; Because 2 + 4 * 2 = 10

This works because the object at the top of the scope of with sticks to the scope of the scope, so this is the first thing to include an inappropriate reference (such as X < / Code>) is checked when resolving, and eval executes the javascript code & nbsp; & Mdash; And so can evaluate expressions & nbsp; & Mdash; And magically happens within the area in which it is called. but beware; As Eric pointed out, not all operators are identical in different forms of expression, and especially for the power of "Bitwise XOR", to interpret ^ in Javascript. (This is not an exponent operator, you have to use Math.pow .)

But you have to be very careful about that thing, both < With / code> and eval (each in its own way) can be problematic. But the main issues of with are that it is difficult to say where something comes from or where you will do an assignment, which is not you; And the main problems with eval come from the use of the string that you do not control. Unless you take security measures and know about the issues ...

boiling in the function:

  function evaluation (string, mapping) {return Str.replace (/ \ # \ {([^ #] +) \} / g, function (match, key) with {var result; (mapping) {result = eval (key);} return result;}) ; } Warning (evaluation ("expression '(# {X} + # {y}) * 2' par '# {(x + y) * 2}'", {"X": 2, "Y": 4} )); // Alert "Expression" (2 + 4) * 2 'equal' 12 "warning (evaluate (" expression '(# {X} + # {y}) * 2' equals' # (X + Y) * 2} '', {"X": 6, "Y": 3})); // Alert "Expression" (6 + 3) * 2 'equal to 18' " 

Comments