Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string with JavaScript

how to parse this string with java script

19 51 2.108997
20 47 2.1089

like this

<span>19 51</span> <span>2.108997</span>     
<span>20 47</span> <span>2.1089</span>
like image 731
mr heLL Avatar asked Jan 06 '12 12:01

mr heLL


People also ask

How do you split a string in JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do I split a word into JavaScript?

You can split a string by each character using an empty string('') as the splitter. In the example below, we split the same message using an empty string. The result of the split will be an array containing all the characters in the message string.

What is the split method in JavaScript?

The split() method takes a pattern and divides a String into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.

How do I split a string in TypeScript?

The split() is an inbuilt function in TypeScript which is used to splits a String object into an array of strings by separating the string into sub-strings. Parameter: This method accepts two parameter as mentioned above and described below: separator – This parameter is the character to use for separating the string.


1 Answers

Assuming you're using jQuery..

var input = '19 51 2.108997\n20 47 2.1089';
var lines = input.split('\n');
var output = '';
$.each(lines, function(key, line) {
    var parts = line.split(' ');
    output += '<span>' + parts[0] + ' ' + parts[1] + '</span><span>' + parts[2] + '</span>\n';
});
$(output).appendTo('body');
like image 119
Rudolph Gottesheim Avatar answered Sep 20 '22 16:09

Rudolph Gottesheim