1

I'm working on a website that uses a datafeed. Within that datafeed (that comes in via a CSV file) there is a paragraph of text. The client is entering the text in bullet point format and would like it to be displayed as such. For example:

  • First point here... * Second point here... * Third point here...

(The client is using the asterix for all points, it's the text editor here converting the first into a bullet point)

Does anyone know a way I'm able to insert line breaks before all but the first asterix so this appears as a list? Maybe with a little javascript?

Ideally the list would appear like this:

  • First point here...
  • Second point here...
  • Third point here...

Thanks in advance,

Tom

6
  • You could just replace the asterisks with an li (as they don't need to be closed): text.replace('*', '<li>'); Commented Jul 14, 2015 at 9:27
  • Create a list (an unordered <ul>) and make list items (<li>) out of all text nodes beginning with asterix. Make the formatting with CSS, you can set different style for the first list element with the selector li:first-child. Commented Jul 14, 2015 at 9:28
  • shouldn't there be a closing tag for <li > too? Commented Jul 14, 2015 at 9:28
  • @user3425760 the closing tag for an li is optional: w3.org/TR/html-markup/li.html Commented Jul 14, 2015 at 9:32
  • Thanks for the suggestion @Pete, how should I insert that into the page, within a script tag I presume? Maybe like this... <script type="text/javascript"> $(document).ready(function () { text.replace('*', '<li>'); }); </script> Commented Jul 14, 2015 at 9:38

3 Answers 3

2

ok I'm guessing here from a mixture of your question and comments but it looks as if the rendered html of your list would be something like this:

<ul>
    <li>First point here... * Second point here... * Third point here...</li>
</ul>

From your comment, it looks as if you are using jQuery so you can do something like this:

$(function() {
    $('li').each(function() {
        var listItem = $(this),
            text =  listItem.text().replace(/\*/g, '<li>');

        listItem.html(text);
    });
});

Example

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

3 Comments

Thanks @Pete, the html code is: <p class="property_description">* First point here... * Second point here... * Third point here...</p>. I've updated the script to focus on .property_description but do you know if there is a way to get the script to also add in an opening and closing <ul> tags, but only in cases where asterix's are featured?
@TomPerkins, I would do something like this: jsfiddle.net/enwwc8q6 but it relies on the asterisk text being the only text in the paragraph
that looks perfect, thank you so much. Please let me know if there's anything I can help you with.
1

Using regular expressions your can write:

input.replace(/\s\*/g, '\n&bull;');

This will replace all * preceeded by space characters by a bullet symbol started from a new line.

If you need to make a real HTML markup out of the input (like li tags), you will have to dig into regexp a little deeper. This MDN page is a great place to start.

Comments

1

Combining the answers you already have, you could use a regular expression to replace the asterisks with <li>. E.g.:

HTML

<div id="output"></div>

Javascript

var str = ' * one * two * three';
str = str.replace(/\s\*/g, '<li>');
$('#output').html('<ul>'+str+'</ul>');

Jsfiddle

Comments

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.