使用javascript分割文本而不会丢失数据

时间:2017-03-07 13:46:12

标签: javascript

我有一个用于拆分文本的代码,但它丢失了一些数据。我的问题是如何拆分文本并保留所有数据。我的代码在下面?

 function splitArray(){
    var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 07.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
    var strArray = myStr.split(" | 0");

    // Display array values on page
    for (var i = 0; i < strArray.length; i++) {
        $("body").append("<p>" + strArray[i] + "</p>");
    }
}

问题在于它在日期7之前删除了数字(0)

| 07.02.2017 13:49:25 |嘉宾6278:大家好,

7.02.2017 13:49:58 |管理员:是的,我可以帮助您

7.02.2017 13:50:25 |访客6278:我们是否需要更改网站

7.02.2017 13:50:58 |管理员:不,不需要

7 个答案:

答案 0 :(得分:4)

您可以将RegExppositive lookahead一起使用。

很少有人问该数字不是以0开头 - 那么您应该使用/(?= \| \d)/。如果您只关心以0开头的数字,请使用/(?= \| 0)/

function splitArray(){
    var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 17.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
    var strArray = myStr.split(/(?= \| \d)/);

    // Display array values on page
    for (var i = 0; i < strArray.length; i++) {
        $("body").append("<p>" + strArray[i] + "</p>");
    }
}
splitArray();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

这是一种不使用正则表达式的方法。它有效,但我认为@Kind user's answer更好。

&#13;
&#13;
var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 07.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
    var strArray = myStr.split("|");

    // Display array values on page
    for (var i = 1; i < strArray.length; i++) {
        $("body").append("<p>" + strArray[i] + " | " + strArray[i + 1] + "</p>");
        i++;
    }
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以在|上拆分,然后将其转换为更好的格式;

var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 07.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
var strArray = myStr.split("|");

var result = [];
for (let i = 0; i < strArray.length; i += 2) {
    result.push(strArray[i] + ' | ' + strArray[i + 1]);
}

//= results should be a array with the entries

答案 3 :(得分:0)

这样做

var strArray = myStr.split(" | ?<=0");

答案 4 :(得分:0)

当然可以,因为它是分隔符的一部分。此外,如果您的日期不是以0开头,那么这将不起作用。因此请尝试将它们成对分开。

未经测试:

function splitArray(){
  var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 07.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
  var strArray = myStr.split('|');
  // remove the first empty element
  strArray.splice(0, 1);

  if (strArray.length % 2 !== 0)
    console.log('Parsing error');

  var messageArray = [];

  while(strArray.length > 0) {
    var pair = strArray.splice(0, 2);
    messageArray.push(pair.join('|'));
  }

  // Display array values on page
  for (var i = 0; i < messageArray.length; i++) {
    $("body").append("<p>" + messageArray[i] + "</p>");
  }
}

答案 5 :(得分:0)

你可以使用正向前看的正则表达式,如果找到管道,空格和数字,则拆分。

    private static void ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // inform the UI that the percentage has been changed
    }

答案 6 :(得分:-1)

只需在追加之前附加一个零:

 function splitArray(){
    var myStr = "| 07.02.2017 13:49:25 | Guest 6278: Hi anybody here | 07.02.2017 13:49:58 | Admin: Yes, how may l help you | 07.02.2017 13:50:25 | Guest 6278: Do we have to change sites | 07.02.2017 13:50:58 | Admin: No, no need";
    var strArray = myStr.split(" | 0");

    // Display array values on page
    for (var i = 0; i < strArray.length; i++) {
    if(i==0)
        $("body").append("<p>" + strArray[i] + "</p>");
        else $("body").append("<p>" +0+ strArray[i] + "</p>");
    }
}
splitArray();