0

I am getting some data back from the server and need to display it using ng-repeat. Here is my json data:

 {
            "eShowOnHome": "No",
            "dModifiedDate": "2016-08-21",
            "eText": "No",
            "eTodayGame": "No",
            "eDelay": "No",
            "eInvert": "None",
            "eResultOrder": "No",
            "iHGameID": "55539",
            "iGameID": "111",
            "vGameTitle": "Billetes Domingo ",
            "eStats": "No",
            "vUrl": "billetes-loteria-nacional",
            "vGameLogo": "https:\/\/s3.amazonaws.com\/cdn.kiskooloterias.com\/dominicanas\/upload\/game_logo\/111\/loteria-nacional-dominicana.jpg",
            "companymodifieddate": "2011-12-19 13:35:54",
            "vCompanyLogo": "https:\/\/s3.amazonaws.com\/cdn.kiskooloterias.com\/dominicanas\/upload\/company\/76\/1373805675_loteria-nacional.jpg",
            "gamemodifieddate": "2015-12-15",
            "iCompanyID": "76",
            "vCompanyName": "Loter\u00eda Nacional",
            "vCompanyurl": "loteria-nacional-resultados",
            "dLastLotteryDate": "2016-08-21",
            "tScore": "23612 +12\r\n01603\r\n21705\r\n63410 +03\r\n02373 +07"
        }

I need the values in the tscore property seperated. I have utilized string split() to split out the values. First, I have to split white space, and then have to split on "\r\n". I have split on white space using the method below and I don't know how to split on "\r\n".

<td><p class="" ><span  ng-repeat="itemcc in item.tScore.split(' ')" >{{itemcc }}</span>

Is there anyway to split both values in single method? Thanks in advance.

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
VibinReji Y
  • 127
  • 1
  • 11

1 Answers1

0

Here is the regex to split \r\n (a new line) or a single space character.

item.tScore.split(/[\r\n | \s]/);

To split with one or more newlines or any number of space characters use the following:

item.tScore.split(/\s+/);
Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
rushi
  • 276
  • 1
  • 6
  • Your code return array with one string, because you split on literal "[\r\n | \s]" and not using regex. Also even if you use regex your regex will be wrong i if you want to split you should use `/\r\n|\s/` or simply `/\s+/` – jcubic Sep 26 '16 at 15:00
  • @jcubic yes what you say is correct. Modified the code with the same. – rushi Sep 26 '16 at 15:02
  • your first regex will split on single character so for string `"foo\r\nbar"` it will return `["foo", "", "bar"]` also you have 2 redundant spaces and it will also split on `|` so for string `"foo|bar"` it will return `["foo", "bar"]`. – jcubic Sep 26 '16 at 15:08
  • I need to split both whitespace and \n\r .But your code is not working. – VibinReji Y Sep 26 '16 at 15:15
  • I have verified the code with your string and it works. Here is the code: var a = "23612 +12\r\n01603\r\n21705\r\n63410 +03\r\n02373 +07"; a.split(/\s+/); ["23612", "+12", "01603", "21705", "63410", "+03", "02373", "+07"] – rushi Sep 26 '16 at 16:13
  • you r using regex pattern to split.But in my case ionic framework not ready to get regex pattern inside split. – VibinReji Y Sep 26 '16 at 20:04
  • If you want a no-regex solution. Try the following, first split the string with "\r\n", now join the array using a space character, then again split using a space. Here is the code: `var a = "23612 +12\r\n01603\r\n21705\r\n63410 +03\r\n02373 +07"; a.split("\r\n").join(" ").split(" "); //["23612", "+12", "01603", "21705", "63410", "+03", "02373", "+07"]` – rushi Sep 27 '16 at 03:08