2

I have an string with HTML format, and I need to replace ALL the math exponents in HTML format <sup></sup>, to math exponents without HTML format.

I'm using the replace() method, but I need find and replace 100 exponents, from <sup>1</sup> to <sup>100</sup>, and I should write all the numbers (from 1 to 100).

var copiar = texto.
                replace(/<br>/g, "\n").
                replace(/<sup><\/sup>/g, "").
                replace(/<sup>2<\/sup>/g, "²").
                replace(/<sup>3<\/sup>/g, "³").
                replace(/<sup>4<\/sup>/g, "⁴").
                replace(/<sup>5<\/sup>/g, "⁵").
                replace(/<sup>6<\/sup>/g, "⁶").
                replace(/<sup>7<\/sup>/g, "⁷").
                replace(/<sup>8<\/sup>/g, "⁸").
                replace(/<sup>9<\/sup>/g, "⁹").
                replace(/<sup>10<\/sup>/g, "¹⁰");
                ...
                replace(/<sup>100<\/sup>/g, "¹⁰⁰");

My question is: There is a way to automate this task? Thanks!

UPDATE: I'm doing this replacements because I'm developing an App for iOS, capable to print (in HTML format) and copy to clipboard (plane text). That's the reason because I'm replacement the <sup> numbers.

UPDATE 14/Oct/2014: I was needing to replace negative exponents too. Using the @minitech answer and modifying a little, I could be able to replace ALL the exponents (positive and negative). Maybe can be useful for someone, here the code:

var map = '⁰¹²³⁴⁵⁶⁷⁸⁹';

var copiar = texto.replace(/<sup>(\-*(\d*))<\/sup>/g, function (str, digits){
    return Array.prototype.map.call(digits, function (digit) {
        var exp = "";

        if (digit != '-') {
            exp += map.charAt(digit);
        } else {
            exp += "¯";
        }
        return exp;
    }).join('');
});
2
  • Copying to clipboard as plain text is probably better done as x^100 instead of x¹⁰⁰, though. I don’t know about your typefaces, but it looks bad in mine… Commented Sep 13, 2014 at 0:32
  • Thanks for your comment @minitech but that is the way I started my App, but how I manage large math expressions it doesn't look so clear. So I decide to change to this way x¹⁰⁰. Commented Sep 13, 2014 at 1:37

3 Answers 3

6

A string and charAt provide a convenient way to map digits to the corresponding superscript digits:

var map = '⁰¹²³⁴⁵⁶⁷⁸⁹';

var copiar = texto.replace(/<sup>(\d*)<\/sup>/g, function (_, digits) {
    return Array.prototype.map.call(digits, function (digit) {
        return map.charAt(+digit);
    }).join('');
});
Sign up to request clarification or add additional context in comments.

5 Comments

Your way is nicer than mine, it doesn't require hard-coding Unicode code points. But I learned some stuff writing mine :)
Thanks a lot! This really solve it, clean and wonderful! You're really good! ;)
@minitech there is a way to replace negative numbers too? Actually I'm needing this but I don't really understand the regExpr. Hope you can can help me!
@JabelMárquez: Do you have a superscript minus character you want to use?
@minitech Actually I think I fixed... I read about regExpr and debug the function (I did my best). I update my question and write the code I use if someone helps. Thanks!
2

When you do a replacement, you can supply a function to calculate it, instead of a fixed string. So you can use a pattern for all your <sup> replacements, and use a function that translates the number to Unicode superscripts.

var copiar = texto.
    replace(/<br>|<sup>(\d*)<\/sup>/g, function(match, digits) {
    if (match == "<br>") {
        return "\n";
    }
    // Rest is for translating superscripts to Unicode
    var superdigits = '';
    var zero = "0".charCodeAt(0);
    var superzero = 0x2070;
    var supertwo = 0x00b2;
    for (var i = 0; i < digits.length; i++) {
        var n = digits.charCodeAt(i) - zero;
        var char;
        switch (n) {
            // Superscripts 2 and 3 are at weird places in Unicode
            case 2: case 3:
                char = String.fromCharCode(n - 2 + supertwo);
                break;
            default:
                char = String.fromCharCode(n + superzero);
        }
        superdigits += char;
    }
    return superdigits;
});

1 Comment

Thanks for your answer, but I pick the minitech's answer because is cleaner and shorter. Thanks anyway, vote up for you!
0
<script>
function get_sup_index(num) {
    var new_num = new String(num);
    new_num =   new_num.replace(/0/g, "⁰").
                        replace(/1/g, "¹").
                        replace(/2/g, "²").
                        replace(/3/g, "³").
                        replace(/4/g, "⁴").
                        replace(/5/g, "⁵").
                        replace(/6/g, "⁶").
                        replace(/7/g, "⁷").
                        replace(/8/g, "⁸").
                        replace(/9/g, "⁹");       
    return new_num;
}

var my_text =   '<sup>1</sup>'+
                '<sup>2</sup>'+
                '<sup>3</sup>'+
                '<sup>4</sup>'+
                '<sup>5</sup>'+
                '<sup>6</sup>'+
                '<sup>7</sup>'+
                '<sup>8</sup>'+
                '<sup>9</sup>'+
                '<sup>10</sup>';

alert(get_sup_index(my_text.replace(/<sup>([0-9]*)<\/sup>/g, "\$1")));
</script>

I hope that can help you.

2 Comments

I'm doing this "conversion" because I'm developing an App for iOS, with a possibility to print and copy to the clipboard. For print do it in HTML, but to copy do it plane. That's the reason.
I have edited my answer, I don't know if you will receive a notification for that. I am writing you just in case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.