dongnan1989 2013-09-19 21:29
浏览 22
已采纳

在文本中查找功能

I'm re-designing a mathematics program for students. The original program was written in adobe flash. This time I am doing it in PHP and JavaScript.

In the original program there are several XML files with hundreds of mathematical questions and some of which are fractions. Those fractions are written as: #(1,4). I want to make an function called # which can make classic fractions.

The problem: there is normal text around the #(1,3) and it is not separated with quotations. How can I let the browser know there is a function in the text? Example:

  <question>what is #(2,8) divided by #(3,4)</question>

I hope you know how to do this. Thanks.

  • 写回答

4条回答 默认 最新

  • duanliaoyu8419 2013-09-19 21:38
    关注

    Without more detailed information:

    var s = "<question>what is #(2,8) divided by #(3,4)</question>";
    var t = s.replace(/(^|\s|>)#\((\d+),(\d+)\)($|\s|<)/g, "$1<span class='fraction'><span class='numerator'>$2</span><span class='denominator'>$3</span></span>$4");
    

    t is now

    "<question>what is <span class='fraction'><span class='numerator'>2</span><span class='denominator'>8</span></span> divided by <span class='fraction'><span class='numerator'>3</span><span class='denominator'>4</span></span></question>"
    

    So: regular expression where you find #(...,...) where the ... are numbers, and the "function" strings are either surrounded by white space, the start/end of the sentence, or angle brackets.

    However, rewriting it to MathJax MathML or LaTeX format (http://mathjax.org) instead will look much nicer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度