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.

1

2 Answers 2

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=''>

Sign up to request clarification or add additional context in comments.

5 Comments

Your code too didn't work either. I'm looking for how to fuse html as the replacement string.
what was your output? Did you get any error message? Was it something similar like {{l.tutorsubject.replace(/,/g, '')}}
It's not displaying anything at all however, when I used <td>{{l.tutorsubject.split(",").join(" ")}}</td>, 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.
Is it possible that you can evaluate the expression in angular controller file? stackoverflow.com/questions/29031157/… stackoverflow.com/questions/19208574/using-regex-in-view
Actually, the {{l.tutorsubject.replace(/,/g, '')}} is from the database along with other results. How do I evaluate it?
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.

1 Comment

There were errors when I used both codes however, when i replaces the </span><span> with another character, it worked. I guess it doesn't want html as the replacement string. How can I use html to replace the ,?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.