如何用' - '?替换空白的额外空格(不是空格)?

时间:2015-01-13 17:17:35

标签: javascript for-loop text replace split

我有这样的字符串:

  

var content =“Lorem ipsum dolor sit amet,consectetur adipiscing   elit,sed do eiusmod tempor incididunt ut labore et dolore magna   aliqua。 Ut enim ad minim veniam,quis nostrud exercitation ullamco   Laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor   在voluptderit in voluptate velit esse cillum dolore eu fugiat nulla   pariatur。 Excepteur sint occaecat cupidatat non proident,sunt in   culpa qui officia deserunt mollit anim id est laborum。“

句子将以左平面html格式显示,如下所示:

  

Lorem ipsum dolor坐下来,念诵adipistur   elit,sed do eiusmod tempor incididunt ut labore et dolore magna   aliqua。 Ut enim ad minim veniam,quis nostrud exercitation ullamco   Laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor   在voluptderit in voluptate velit esse cillum dolore eu fugiat nulla   pariatur。 Excepteur sint occaecat cupidatat non proident,sunt in   culpa qui officia deserunt mollit anim id est laborum。

如何使用JavaScript将行末尾的任何剩余额外空格替换为否定( - )字符,这使得句子对齐,所以我得到这样的结果?

  

Lorem ipsum dolor坐下来,念诵adipistur   elit,sed do eiusmod tempor incididunt ut labore et dolore magna   aliqua。 Ut enim ad minim veniam,quis nostrud exercitation ullamco   Laboris nisi ut aliquip ex ea commodo consequat。 Duis aute irure dolor   在voluptderit in voluptate velit esse cillum dolore- eu fugiat nulla   pariatur。 Excepteur sint occaecat cupidatat non proident,sunt in   culpa qui officia -------- deserunt mollit anim id est laborum .-------------------------------- -------------------------------------------------- -

输出应该是这样的output

必须保留报告的原始格式,因此不允许更改

我尝试使用此代码jsfiddle

实现这一目标
var html = content.split(" ");
var htmlTmp = "";
var result = "";
var limitlength = 40;
var linelength = 50;

for(var i=0; i< html.length; i++) {    
    var htmlTmp_length = htmlTmp.length;
    if(htmlTmp_length > limitlength) {
        result += html[i] + " ";

        if(htmlTmp_length < linelength) {
            for(j=0;j<(linelength - htmlTmp_length);j++) {
                result += "-";
            }
        }

        result += "<br/>";
        htmlTmp = "";                                        
    }
    else {
        result += html[i] + " ";
        htmlTmp += html[i] + " ";
    }    
}

$(".content2").html(result);

但它似乎不起作用,因为每个字母占用不同的空间。

1 个答案:

答案 0 :(得分:1)

这是你需要的吗?

var LIMIT = 1<<16;
function twTexts (target, content, delim, cw, noOverflow) {
    // Split text into delim
    var array = content.split(delim);
    var span = $("<span></span>");
    target.append(span);
    var loopCount = 0;
    for (var i = 0; loopCount++ < LIMIT && i < array.length; i++) {
        if (i > 0) span.append(delim);
        span.append(array[i]);
        if ((noOverflow && span.width() > target.width()) || span.height() > cw) {
            // Line break caused by this word.
            var text = array.slice(0, i).join(delim);
            span.text(text);
            if (i != 0) {
                // Add '-' until the line breaks
                for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                    span.append('-');
                span.text(text.substring(0, text.length - 1));
                span.append("<br>");
                array = array.slice(i);
            } else {
                // Oops. The line break has happened because of the first word!
                if (array[0].length > 1) {
                    // Let's just break this into characters
                    console.log("Called twTexts");
                    twTexts (target, array[0], '', cw, noOverflow);
                } else {
                    // Ooops. the container's width is narrow than characters. just append this one.
                    span.text (array[0]);
                }
                array = array.slice(i + 1);
            }
            // Start new line
            span = $("<span></span>");
            target.append(span);
            i = -1;
        } else if (i + 1 >= array.length) {
            // No line break occured and this is the last word.
            text = array.join(delim);
            for (;span.height() <= cw && span.width() <= target.width(); text += '-')
                span.append('-');
            span.text(text.substring(0, text.length - 1));
            span.append("<br>");
            break;
        }
    }
}

var content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.fsdfsfdfsfsffaddfadf";

// The target
$(".target").each(function (i, e) {
    var target = $(e).empty();

    // Calculate the height of a character
    var span = $("<span>A</span>");
    target.append(span);
    var cw = span.height();
    span.remove();

    // Print text
    twTexts (target, content, ' ', cw, true);
});

JSFiddle:http://jsfiddle.net/wnalfl3737/nkf3ob55/6/