I have a string like 5 1/36
that I want to rewrite as 5 <sup>1</sup>⁄<sub>36</sub>
. Before the fraction, there can either be nothing or a space, and after the fraction there can either be a space or nothing. A string could contain multiple fractions. The fractions may be positive or negative.
example:
myString = '(1 3/4)÷(-2/3)-(-6)-(6)-(1)+(-4)';
Like this:
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>');
Here's a working example:
myString = '(1 3/4)÷(-2/3)-(-6)-(6)-(1)+(-4)'; document.write(myString.replace(/(\\d+)(\\/)(\\d+)/g, '<sup>$1</sup>⁄<sub>$3</sub>'))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.