0

I have and angularjs view in html below

<tr ng-repeat="l in tutorRequests" class="form-text">
    <td>{{l.tutorid}}</td>
    <td>{{l.tutorsubject.replace(',',"")}}</td>
    <td>View Details</td>

the Output of the <td>{{l.tutorsubject}}</td> is in the format below:

Further Mathematics,Environmental Management,Geography ,Programming,Physics,

When I used <td>{{l.tutorsubject.replace(',',"")}}</td> it replaced the first comma in the output string. How can I replace all the comma character in the entire string? The idea is to replace the comma with a </span><span class="">

Would be waiting for your response.

4castle
  • 32,613
  • 11
  • 69
  • 106
Guzzyman
  • 561
  • 6
  • 16
  • 37
  • See [Replacing all occurences of a string in JavaScript](http://stackoverflow.com/questions/1144783/replacing-all-occurrences-of-a-string-in-javascript) – 4castle May 25 '16 at 17:12

2 Answers2

3
<td>{{l.tutorsubject.replace(/,/g,"</span><span class=''>")}}</td>

The regex /,/g matches all occurrence of comma in the string and replace with </span><span class=''>

Daniel Netto
  • 387
  • 5
  • 18
  • Your code too didn't work either. I'm looking for how to fuse html as the replacement string. – Guzzyman May 25 '16 at 18:03
  • what was your output? Did you get any error message? Was it something similar like `{{l.tutorsubject.replace(/,/g, '')}}` – Daniel Netto May 25 '16 at 18:31
  • It's not displaying anything at all however, when I used `{{l.tutorsubject.split(",").join(" ")}}`, I got result with space in between the subjects. I want the span html between the subjects so I can style it with padding, background color and margin to make the subjects look seprate and visually appealing. – Guzzyman May 26 '16 at 07:37
  • Is it possible that you can evaluate the expression in angular controller file? http://stackoverflow.com/questions/29031157/angularjs-string-replace-in-html http://stackoverflow.com/questions/19208574/using-regex-in-view – Daniel Netto May 26 '16 at 08:19
  • Actually, the `{{l.tutorsubject.replace(/,/g, '')}}` is from the database along with other results. How do I evaluate it? – Guzzyman May 26 '16 at 08:47
2

You can either use a regex with the global modifier /,/g:

<td>{{l.tutorsubject.replace(/,/g,"</span><span>")}}</td>

Or you can use the functional alternative:

<td>{{l.tutorsubject.split(",").join("</span><span>")}}</td>

Note: Setting class="" can be omitted because having no classes is the default.

4castle
  • 32,613
  • 11
  • 69
  • 106
  • There were errors when I used both codes however, when i replaces the `` with another character, it worked. I guess it doesn't want html as the replacement string. How can I use html to replace the `,`? – Guzzyman May 25 '16 at 18:02