Split string and display result accordingly using javascript?

javascript split regex
javascript split string
javascript split string by comma
jquery split string to array
javascript split string at index
javascript split array
javascript substring
split string and store in array javascript

I have data which contains a combination of repeating strings.

var data = 01.01.2019 11:11:39 Sahil Dhir(K21004)  test1, 
    02.01.2019 14:10:02 Rahul Kumar(K210005)  test2,
    03.01.2019 15:52:36 Jhonny Sins(K21006) test3,
04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4

I want to display the data in a table or div format.

This is what I have tried so far.

var data = `01.01.2019 11:11:39 Sahil Sharma(K21004)  test1, 
    02.01.2019 14:10:02 Rahul Verma(K210005)  test2,
    03.01.2019 15:52:36 Shikha Kappor(K21006) test3,
04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4`;
    var arr = new Array();
    arr = data.split(',');
    var result = " " ;
    for(var i=0; i<arr.length; i++){
    result += ""+arr[i]+"";
    }
    console.log(result);
    var e = "";
    e = result.split(/[]+/);
    document.getElementById('print').innerHTML = "<span>"+e+ "</span></br>";
<div id="print"></div>

With this way, you can split your string as you like. If you want you can split splitArray values separately by space and create table as you want.

var data = `01.01.2019 11:11:39 Sahil Sharma(K21004)  test1, 
    02.01.2019 14:10:02 Rahul Verma(K210005)  test2,
    03.01.2019 15:52:36 Shikha Kappor(K21006) test3,
04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4`;


data = data.replace(/\n/g, ' ');
data = data.replace(/\s+/g, ' ');
data = data.replace(/\(/g, ' (');
data = data.replace(/\,\s+/g, ',');

let splitArray = data.split(',');

console.log(JSON.stringify(splitArray))

let text = ''

for(let a = 0; a < splitArray.length; a++ ) {

  text += splitArray[a].split(' ').join(' | ') + '</br>';

}

document.getElementById('print').innerHTML = text
<div id="print"></div>

Split() method to create array by breaking string using delimiterin in , Creating array by breaking strings using delimiters in split function. Array Split We have seen how to create string using elements of an array. Now we will see how to Here is the output <script type="text/javascript"> display elements /// string is the source string variable, separator is the delimiter to split source string and limit defines an upper limit on the number of splits to be found in a given string. Items after the split limit will not be included in the return array.

Split and join on (

var data = '01.01.2019 11:11:39 Sahil Sharma(K21004)  test1, 02.01.2019 14:10:02 Rahul Verma(K210005)  test2,03.01.2019 15:52:36 Shikha Kappor(K21006) test3,04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4';
    var arr = new Array();
    arr = data.split(',');
    var result = " " ;
    for(var i=0; i<arr.length; i++){
    var e = "";
    e = " "+arr[i].split('(').join(" (")+" ";
   
    document.getElementById('print').innerHTML+= "<span>"+e+ "</span></br>";
    }
<div id="print"></div>

JavaScript String split() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java� The split() method is used to split a string into an array of substrings, and returns the new array. Tip: If an empty string ("") is used as the separator, the string is split between each character. Note: The split() method does not change the original string.

Split data on , space and (.

processedArray will contain purified elements that you can iterate over to add them to your table.

let data = `01.01.2019 11:11:39 Sahil Sharma(K21004)  test1, 
    02.01.2019 14:10:02 Rahul Verma(K210005)  test2,
    03.01.2019 15:52:36 Shikha Kappor(K21006) test3,
04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4`;
let rows = data.split(",")
let array = [];
let elem;
let processedArray = [];
rows.forEach(function(row){
    array.push(row.trim().split(" "));
});
array.forEach(function(i){
    for(j = 0; j < i.length; j++){
        if(i[j] !== ""){
            elem = i[j].split("(");
            elem.forEach(function(k){
                processedArray.push(k.replace(")",""));
            });
        }
    }
});
console.log(processedArray);

JavaScript function: Returns a passed string with letters in , This site uses cookies to deliver our services and to show you relevant ads. By Write a JavaScript function that returns a passed string with letters in alphabetical order. Example string: 'webmaster' Expected Output: 'abeemrstw' The split() method is used to split a String object into an array of strings by� In other languages I use String.splits' limit argument usually means, "split this string this many times, and put any remainder as the last array index". In Javascript however anything past the limit split is just discarded. Example: /* A function that splits a string `limit` times and adds the remainder as a final array index.

try something like this to add the content in a table:

 <div id="content">content</div>


 <script>
        String.prototype.insert = function (index, string) {
  if (index > 0)
    return this.substring(0, index) + string + this.substring(index, this.length);

  return string + this;
};
        var data = "01.01.2019 11:11:39 Sahil Dhir(K21004)  test1,02.01.2019 14:10:02 Rahul Kumar(K210005)  test2,03.01.2019 15:52:36 Jhonny Sins(K21006) test3,04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4";

var position = data.indexOf('(');
data= data.insert(position, " ");
while (position !== -1) {

  position = data.indexOf('(', position + 2);
  data= data.insert(position, " ");
}
var sp=data.split(',');
var text="<table>";
text+="<tr><th>date</th><th>time</th><th>first name</th><th>last name</th><th>pin</th><th>test no</th></tr>";
for (var i=0;i<sp.length;i++){
    text+="<tr>";
    var spi=sp[i].split(" ");
    for(var j=0;j<spi.length;j++){
        text+="<td>"+spi[j]+"</td>";
    }
    text+="</tr>";
}
text+="</table>";
document.getElementById('content').innerHTML = text;
    </script>

Split Method in Java: How to split a String using split() Method , There are many ways to split a string in Java but the most common way is regex – a delimiting regular expression; limit – the resulting threshold of String objects computed by splitting the given string according to limit parameter Pointers in C � Linux commands � Android tutorial � JavaScript tutorial� There are many ways to split a string in Java. The most common way is using the split() method which is used to split a string into an array of sub-strings and returns the new array. 1. Using String.split ()¶ The string split() method breaks a given string around matches of the given regular expression. There are two variants of split() method

If the data is all the same - you can take liberties with the splitting and get the elements. If the data structure changes - this will not work. Note that I have amended the datas - to remove the leading spaces and the newline characters from your code.

The following creates an aray of objects which can then be iterated over to give the table structure - with each object containing the portions of the data item.

var data = `01.01.2019 11:11:39 Sahil Sharma(K21004)  test1,02.01.2019 14:10:02 Rahul Verma(K210005)  test2,03.01.2019 15:52:36 Shikha Kappor(K21006) test3,04.01.2019 15:52:36 Mahendra Bahubali(K21006) test4`;
  var newArr = [];

    var  arr = data.split(',');
        
    arr.forEach(function(item){
      var newObj = {};
      var portions = item.split(' ');
      newObj['date'] = portions[0];
      newObj['time'] = portions[1];
      newObj['firstName'] = portions[2];
      newObj['lastName'] = portions[3].split('(')[0];
      newObj['code'] = portions[3].split('(')[1].split('\)')[0];
      newObj['test'] = item.split(')')[1].trim();
      newArr.push(newObj)
    });
    
    var tableStr = '';
    
    newArr.forEach(function (item){
    tableStr += '<tr>';
      tableStr += '<td>' + item.date + '</td>';
      tableStr += '<td>' + item.time + '</td>';
      tableStr += '<td>' + item.firstName + '</td>';
      tableStr += '<td>' + item.lastName + '</td>';
      tableStr += '<td>' + item.code + '</td>';
      tableStr += '<td>' + item.test + '</td>';
    tableStr += '</tr>';
    
    })
    
    document.querySelector('#data-table tbody').innerHTML = tableStr;
table {
  border-collapse: collapse;
}

table, th, td {
  border: 1px solid black;
  text-align: left;
  padding: 5px 10px;
}
<table id="data-table" >
  <thead>
    <tr>
      <th>Date</th>
      <th>Time</th>
      <th>First name</th>
      <th>Last Name</th>
      <th>PID</th>
      <th>Test No.</th>
    </tr>
  </thead>
  <tbody></tbody>
</table>

How to Split a String in Java, The most common way is using the String.split () method, also see how to use the pattern is applied and therefore affects the length of the resulting array). void main(String[] args) { String url = "http://www.w3docs.com/learn-javascript. html";� From the Mozilla Developer Network for function split():. The split() method returns the new array. When found, separator is removed from the string and the substrings are returned in an array.

JScript, To deal with strings, TestComplete has a special aqString scripting object. Replace, Replaces all the occurrences of one substring with another substring. In this case all other operands are converted to a string and the resulting string is formed. The comparison is case-sensitive and is made according to local settings. If we use for..of to loop over matchAll matches, then we don’t need Array.from any more. str.split(regexp|substr, limit) Splits the string using the regexp (or a substring) as a delimiter. We can use split with strings, like this:

How to Use the JavaScript Split Method to Split Strings and String , The string in JavaScript allows you to store information within a string or an object . Portions of the string that remain after the split will not be included in the resulting array. Our first example will show you what happens when a string is split without specifying any criteria. The output of the above would therefore be: � JavaScript Display Possibilities. JavaScript can "display" data in different ways: Writing into an HTML element, using innerHTML. Writing into the HTML output using document.write(). Writing into an alert box, using window.alert(). Writing into the browser console, using console.log().

String, The String object wraps JavaScript's string primitive data type with the A similar result can be achieved using the localeCompare method inherited by and a string, and to replace the matched substring with a new substring. the value of the string converted to lower case according to any locale-specific case mappings.

Comments
  • split arr[i] by space
  • @DananjayaAriyasena can't because he want to separate Sharma(K21004) too