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

How can I replace blank extra spaces (not white-spaces) with '-'?

我有这样的字符串:

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."

句子会以左平html格式显示,像这样:

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.

如何使用 JavaScript 将行末剩余的任何多余空白 space 替换为负 (-) 字符,从而使句子对齐对齐,从而得到这样的结果?

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.-----------------------------------------------------------------------------------

输出应该是这样的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);

但是好像不行,因为每个字母占用的空间都不一样space。

这是你需要的吗?

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/