Search code examples
javascriptregextex

Javascript Regex for TeX enclosed by $$


Regex Experts!

Considering we have the following text, I'm attempting to find a REGEX to detect all TeX expressions enclosed by two $$s.

Select the correct answer:
  a) 10 % = $$\frac{1}{10}$$.
  b) 33 % < $$\frac{1}{3}$$
  c) 2 % = $$\frac{2}{100}$$,
  d) 9 % < $$\frac{1}{9}$$
  e) 5 % = $$1+\frac{1}{5}$$
  f) 1 % = $$0.05 - \frac{1}{5}$$

So far, what I have come up is:

/^\${2}[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/ ?]*(\${2}|[\n\r]| )$/g

which, as you might have anticipated, shamefully isn't working! Here are a few issues I have: first, I am having difficulty to detect \frac or for that matter \. Although I have added \\, that still fails. Second, I am having problem with the fact that $$ could be followed by space, newline, , or . So even if I specify my expression to look for $$ at the end, that still won't catch many of cases.

I know I am not the first person who has ever attempted this so please share your thoughts or recommendations. Thanks in advance for your help!


Solution

  • Your ^\${2}[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/ ?]*(\${2}|[\n\r]| )$ pattern is too verbose. Instead of whitelisting all possible chars there can appear in betweeb double dollar symbols, you may assume there may be any char there.

    You may use

    /\${2}.*?\${2}/g
    

    Or - if there can be line breaks in between (which I doubt):

    /\${2}[\s\S]*?\${2}/g
    

    See the regex demo.

    JS demo:

    var rx = /\${2}.*?\${2}/g;
    var str = "Select the correct answer:\n  a) 10 % = \$\$\\frac{1}{10}\$\$.\n  b) 33 % < $$\\frac{1}{3}$$\n  c) 2 % = $$\\frac{2}{100}$$,\n  d) 9 % < $$\\frac{1}{9}$$\n  e) 5 % = $$1+\\frac{1}{5}$$\n  f) 1 % = $$0.05 - \\frac{1}{5}$$";
    console.log(str.match(rx));
    // => ["$$\\frac{1}{10}$$","$$\\frac{1}{3}$$","$$\\frac{2}{100}$$","$$\\frac{1}{9}$$","$$1+\\frac{1}{5}$$","$$0.05 - \\frac{1}{5}$$"]
    //or, to get what is inside
    
    var rx_extract = /\${2}(.*?)\${2}/g;
    var m, results = [];
    while (m=rx_extract.exec(str)) {
      results.push(m[1]);
    }
    console.log(results);
    // => ["\\frac{1}{10}","\\frac{1}{3}","\\frac{2}{100}","\\frac{1}{9}","1+\\frac{1}{5}", "0.05 - \\frac{1}{5}"]