本文介绍了如何使用jquery将插入符号后的连续数字转换为上标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与将插入符号转换为上标后,我得到了一个非常。

This question is related to How to convert numbers after caret to superscript with jquery where I got a very good answer.

现在问题,我需要扩展,以便将连续的数字考虑在内。

Problem now, I need to extend the recent script | jsfiddle so that it takes successive numbers into account.

最近的脚本需要一个字符串,并检查它是否为插入符号。然后:

The recent script takes a string, and checks it for carets. Then:


  1. 如果插入符号后面没有括号,则只标注下一个字符。 x ^ 2 变为 x< sup> 2< / sup>

  2. 如果有一个开口支架,则上标直至关闭支架。 x ^(2y + 1)变为 x< sup> 2y + 1< / sup>

  1. If there is no bracket behind the caret, only superscript the next character. x^2 becomes x<sup>2</sup>
  2. If there is an opening bracket, superscript until the closing bracket. x^(2y+1) becomes x<sup>2y+1</sup>

但是,我需要延长n°1,因为可能有一个数字持有几位数甚至变量即可。

However, I need to extend n°1 as there might be a number holding several digits or even variables.

所以我们需要一个新的n°1:

So we need a new n°1:


  1. 如果没有只要它们是数字或字符,就可以在插入符号后面加上所有连续字符。因此 x ^ 12a 变为 x< sup> 12a< / sup>

  1. If there is no bracket behind the caret, superscript all successive characters as long as they are numbers or characters. So that x^12a becomes x<sup>12a</sup>

我试图实现它,包括变量 afternext = input [(i + 1)])并检查这个char by if(afternext.match(/ ^ [0-9a-zA-Z] + $ /)){...} 存储它,但我失败了:(

I tried to implement it, including a variable afternext = input[ (i+1) ]) and checking this char by if(afternext.match(/^[0-9a-zA-Z]+$/)) { ... } storing it, but I failed :(

所以,如果今天有人觉得合适,我很乐意看到你的解决方案。

So if anyone feels fit enough today, I'd be happy to see your solution for that.

谢谢。

推荐答案

。我重构了一下使用内部循环而不是缓冲数组,因为它有额外的缓冲条件有点混乱。

Here you go, sir. I refactored it a bit to use an inner loop instead of a buffer array, since it got a bit messy with the additional buffering condition.

// transform carets, e.g. x^2 to x<sup>2</sup> and x^(2y) to x<sup>2y</sup>
function superify(input) {
    if(!input) return input;
    var output = [];
    var alphanumeric = /^[a-z0-9]+$/i;
    var i = 0;
    var parens;

    //outer loop for iterating string
    while(i < input.length) {
        var current = input[i++];
        var next    = input[i];

        if(current === '^' && next && ((parens = (next === '(')) || next.match(alphanumeric))) {

            var first = next;

            //start super
            output.push('<sup>');
            if(!parens) 
                output.push(first);

            //push chars to output until break or string end
            while(i++ < input.length) {
                current = input[i];
                if(parens && current === ')')
                    break;
                if(!parens && !current.match(alphanumeric))
                    break;
                output.push(current);
            }

            output.push('</sup>');
            if(!parens) 
                output.push(current);
            i++;
        }
        else {
            output.push(current);
        }
    }

    return output.join('');
}

这篇关于如何使用jquery将插入符号后的连续数字转换为上标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 09:43