0

Is there a simple way of converting a sting like <p>{"3+5/2"}</p> or anything with only mathematical symbols to just <p>{3+5/2}</p> so it displays the result?

Victor Lazaro
  • 545
  • 2
  • 6
  • 16
  • 4
    well there's always `eval` - but you should always think twice, or a hundred times, before using that. Where are you actually getting that string from in the first place? – Robin Zigmond Sep 30 '20 at 19:28
  • You can use a [Wolfram Alpha API](https://products.wolframalpha.com/api/). You can send your formula there and get the result using AJAX calls. – Thomas Sablik Sep 30 '20 at 19:30
  • 2
    I would be ‘very surprised’ if there are not suitable JS libraries, for parsing and evaluating various mathematical strings, which can be found with a little bit of earnest searching. As far as this question - searching on SO would reveal duplicates. See https://www.google.com/search?q=javascript+evaluate+math+expression which finds questions like https://stackoverflow.com/q/2276021/2864740 – user2864740 Sep 30 '20 at 19:31
  • One of the many libs for this (and much more): [mathjs](https://mathjs.org/) – trincot Sep 30 '20 at 19:32
  • Yes, there is a simple way. – Travis J Sep 30 '20 at 19:36
  • @user2864740 Thanks! I swear I did a search both in SO an Google. I guess I couldn't find the right terms to search. – Victor Lazaro Sep 30 '20 at 19:59
  • Thanks everyone for your answers. I'm going to go with [mathjs](https://mathjs.org/examples/expressions.js.html) and `math.evaluate` – Victor Lazaro Sep 30 '20 at 20:23

0 Answers0