Reputation: 184
I am trying to replace a string with two fractions "x={2x-21}/{x+12}+{x+3}/{x-5}" with a string "x=\frac{2x-21}{x+12}+\frac{x+3}{x-5}" (i.e. convert from jqMath to LaTex).
To achieve this, I've written the following code:
var initValue = "(\{.*\}(?=\/))\/(\{.*\})";
var newValue = "\\frac$1$2";
var re = new RegExp (initValue,"g");
var resultString = givenString.replace(re,newValue);
return resultString;
This code seems to work for strings with just one fraction (e.g. "x={2x-21}/{x+12}") but when I try to apply it to the example with two fractions, the result is x=\frac{2x-21}/{x+12}+{x+3}{x-5}. As far as I understand, regex engine captures {2x-21}/{x+12}+{x+3} as the first group and {x-5} as the second group. Is there any way to get the result that I want with regular expressions?
The same question is applicable to other patterns with several non-nested delimiters, for example: "I like coffee (except latte) and tea (including mint tea)". Is it possible to capture both statements in parentheses?
If not, I will probably have to write a function for this (which is ok, but I wanted to make sure this is the right approach).
Upvotes: 1
Views: 70
Reputation: 67968
({[^}]+})\/({[^}]+})
Try this.See demo.Repalce by \\frac$1$2
.
http://regex101.com/r/tF5fT5/24
Upvotes: 1