HTML-ifying Text in SharePoint
This has probably been done to death, but here’s my version of how to “HTML-ify” HTML text in SharePoint calculated fields into rendered HTML using jQuery:
1 2 3 4 5 6 7 8 9 10 11 |
<span style="font-family: Lucida Console,monospace;"><span style="color: #4d70ea;">var </span><span style="color: #e87400;">htmlPattern = /[^<]*<([^ ]+)[^<]+<\/\1>.*/;</span> <span style="color: #4d70ea;">var </span><span style="color: #e87400;">ltPattern = /&lt;/g;</span> <span style="color: #4d70ea;">var </span><span style="color: #e87400;">gtPattern = /&gt;/g;</span></span> <span style="font-family: Lucida Console,monospace;"><span style="color: #e87400;">$(selectors).each(</span><span style="color: #4d70ea;">function</span><span style="color: #e87400;">(index, item){</span> <span style="color: #4d70ea;">var </span><span style="color: #e87400;">html = $(item).html().replace(ltPattern, </span><span style="color: #a56dbc;">"<"</span><span style="color: #e87400;">).replace(gtPattern, </span><span style="color: #a56dbc;">">"</span><span style="color: #e87400;">);</span></span> <span style="font-family: Lucida Console,monospace;"> <span style="color: #4d70ea;">if</span><span style="color: #e87400;">(!htmlPattern.test(html)) { </span><span style="color: #4d70ea;">return</span><span style="color: #e87400;">; }</span></span> <span style="font-family: Lucida Console,monospace;"> <span style="color: #e87400;">$(item).html(html);</span> <span style="color: #e87400;">});</span></span> |
Perhaps the most interesting part of this script is the regular expression used...