![](/img/trans.png)
[英]I am trying to make a Fraction Calculator in HTML Javascript, I need to simplify the fraction how can I do this?
[英]How can I reformat 1 1/2 into pretty fraction HTML using regex and javascript
我有一個像5 1/36
這樣的字符串,我想將其重寫為5 <sup>1</sup>⁄<sub>36</sub>
。 在小數之前,可能沒有任何內容或有空格,在小數之后可能沒有空間或沒有任何內容。 一個字符串可以包含多個分數。 分數可以是正數或負數。
例:
myString = '(1 3/4)÷(-2/3)-(-6)-(6)-(1)+(-4)';
像這樣:
var regex = /(\d+)\/(\d+)/g;
var myString = "(1 3/4)÷(-2/3)-(-6)-(6)-(1)+(-4)";
var myResult = myString.replace(regex, "<sup>$1</sup>⁄<sub>$2</sub>");
// "(1 <sup>3</sup>⁄<sub>4</sub>)÷(-<sup>2</sup>⁄<sub>3</sub>)-(-6)-(6)-(1)+(-4)"
一個簡單的字符串替換數字有關/
應該可以解決問題
str = str.replace(/\b(\d+)\/(\d+)/g, '<sup>$1</sup>⁄<sub>$2</sub>');
這是一個工作示例:
myString = '(1 3/4)÷(-2/3)-(-6)-(6)-(1)+(-4)'; document.write(myString.replace(/(\\d+)(\\/)(\\d+)/g, '<sup>$1</sup>⁄<sub>$3</sub>'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.