简体   繁体   English

如何在JavaScript中覆盖RegExp?

[英]How do I override RegExp in Javascript?

I'm using a Javascript library called MathJax which allows you to use LaTex and other mathematic markup languages like it in HTML. 我正在使用一个名为MathJax的Javascript库,该库允许您在HTML中使用LaTex和其他数学标记语言。 I'm trying to use it in Javascript but the regular expressions are getting in the way. 我正在尝试在Javascript中使用它,但是正则表达式正在妨碍您。 Here is my code: 这是我的代码:

function sendTex()
{
    var latexStuff = $("#texField").val();
    var latexString = "When $a \ne 0$, there are two solutions to \*(ax^2 + bx + c = 0\*)       and they are
    $$x = {-b \pm \qrt{b^2-4ac} \over 2a}.$$";
    $("#texVersion").append(latexString);
}

Once you add new mathematics to the page, you need to tell MathJax to process that new math. 在页面上添加新数学后,您需要告诉MathJax处理该新数学。 See the MathJax documentation for details. 有关详细信息,请参见MathJax文档 The main idea is to use 主要思想是使用

MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

when you want MathJax to process new math on the page. 当您希望MathJax在页面上处理新数学时。 In your case, you might try 就您而言,您可以尝试

function sendTex()
{
  var latexStuff = $("#texField").val();
  var latexString = "When $a \\ne 0$, there are two solutions to \\(ax^2 + bx + c = 0\\)" +
    " and they are $$x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}.$$";
  $("#texVersion").append(latexString);
  MathJax.Hub.Queue(["Typeset",MathJax.Hub,"texVersion"]);
}

and see if that works better for you. 看看是否更适合您。 Note that you need to double your backslashes, since in JavaScript strings, a backslash is a special character, so to get a literal backslash you need to use two of them. 请注意,您需要将反斜杠加倍,因为在JavaScript字符串中,反斜杠是特殊字符,因此要获得文字反斜杠,您需要使用两个反斜杠。 You also had \\qrt rather than \\sqrt , and \\*(...\\*) rather than the usual \\(...\\) (but perhaps you have configured MathJax to use alternative delimiters). 您还可以使用\\qrt而不是\\sqrt ,还可以使用\\*(...\\*)而不是通常的\\(...\\) (但是您可能已经配置了MathJax以使用其他定界符)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM