第n次出现换行符后剪切字符串(性能)

时间:2018-09-25 22:22:00

标签: javascript jquery html css performance

我目前正在尝试寻找一种在div中切片换行符的有效方法。 我可以使用任何人推荐的东西,只要它高效且不需要JQuery的任何JavaScript框架即可。

我制作了以下工作原型,但我不知道它与其他方法相比有多有效。 JSFiddle

(function($) {
  $(document).ready(function(){
    $('#Lines').on('change', function(){
      $('#Entry').change()
    });
    
    $('#Entry').on('change', function(){
      var _a = $(this).val(),
      _d = '\n',
      _s = $('#Lines').val(),
      _t = _a.split(_d).slice(0, _s),
      _r = _t.join(_d);

      $('#Original > div.Data').text(_a);
      $('#Modified > div.Data').text(_r);
    });
    
    $('#Entry').val('1 Line\n2 Line\n3 Line\n4 Line\n5 Line\n6 Line\n7 Line\n8 Line\n9 Line\n10 Line').change();
  });
})(jQuery);
body {
  font-family: Roboto;
}

.Data {
  white-space: pre-wrap;
}

.Title {
  font-size: 20px;
  font-weight: 600;
}

.input {
  display: inline-block;
  float: right;
}

#Original,
#Modified,
#Entry,
.input{
  -webkit-box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
  box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14),
    0 3px 1px -2px rgba(0, 0, 0, 0.12), 0 1px 5px 0 rgba(0, 0, 0, 0.2);
  padding: 10px;
}

.border-box {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.w-100 {
  width: 100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div class='input'>
    NUMBER OF NEW LINES TO SLICE:
  <input type="number" name="Lines" id="Lines" value='2'>
  </div>
  <div class='w-100'>
    <textarea id="Entry" class="w-100 border-box" rows="10"></textarea>
  </div>
</div>
<br/>

<div id='Modified'>
  <div class='Title'>Modified</div>
  <br/>
  <div class='Data'></div>
</div>

<br/>

<div id='Original'>
  <div class='Title'>Original</div>
  </br>
  <div class='Data'></div>
</div>

2 个答案:

答案 0 :(得分:1)

我认为最快的代码将是避免额外的操作,例如字符串拆分/连接或正则表达式。您可以使用indexOf查找\ n,获取您感兴趣的最后一个\ n的索引,然后执行一次.slice()调用。如果发现所需的\ n个字符较少,则只需返回完整字符串即可。

function splitLine(str, countLines) {
    if (!str) {
        return str; 
    }
    if (countLines <= 0) {
        return '';
    }
    let nlIndex = -1;
    let newLinesFound = 0;
    while(newLinesFound < countLines) {
        let nextIndex = str.indexOf('\n', nlIndex + 1);
        if (nextIndex === -1) {
            return str;
        }
        nlIndex = nextIndex;
        newLinesFound++;
    }
    return str.slice(0, nlIndex);
}

答案 1 :(得分:0)

您可以使用RegExp

let numberOfLines = 3;

let lines = `1 Line
2 Line
3 Line
4 Line
5 Line
6 Line
7 Line
8 Line
9 Line
10 Line`;

let splitter = new RegExp(`(.*?\n){${numberOfLines}}`);

let matches = splitter.exec(lines);

if (matches)  console.log(`Lines splited: ${matches[0]}`);

输出:

Lines splitted: 1 Line
2 Line
3 Line

如果存在任何以某事开头并以\n结尾的模式重复numberOfLines次,则matches[0]将是该字符串。

实施代码看起来像

(function($) { 
    $(document).ready(function() {

        $('#Entry, #Original > div.Data').text(`1 Line
2 Line
3 Line
4 Line
5 Line
6 Line
7 Line
8 Line
9 Line
10 Line`);

        $('#Lines, #Entry').on('change', function() {
        var numberOfLines = $('#Lines').val();
        var newModifiedText;

        var splitter = new RegExp(`(.*?\n){${numberOfLines}}`);

        var matches = splitter.exec($('#Entry').val());
        if (!matches) {
            newModifiedText = "Not enough lines to be splitted in the 'entries' textbox!";
        } else {
          newModifiedText = matches[0];
        }

        $('#Modified > div.Data').text(newModifiedText);
    });
  });
})(jQuery);
相关问题