Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:44:29+00:00 2026-06-13T07:44:29+00:00

I’m looking at a very small amount of code: var val = $("#id_input").val(); $("#output").text(val);

  • 0

I’m looking at a very small amount of code:

var val = $("#id_input").val();
$("#output").text(val);

Which essentially takes the input into a field, <textarea id="id_input"></textarea>, and outputs it, exactly as it is.

What I’m trying to do is turn input newlines that begin with a - into output <ul><li></li></ul> on my site….

The approach I’ve been going at is to split the input by lines and then concatenate them, after passing each line through this:

function startsWith(string, pattern) {
  return string.slice(0, pattern.length) == pattern;
}

show(startsWith("-"));

I feel like there’s a more standard approach though? For example, I’ve read other posts on StackOverflow that use a find function to produce similar results. I’m suspicious of these because there’s no actual regex. It seems too good to be true.

enter image description here

In the image, you can see that green text is comments, white text is input, and black text is output.

I understand that there are existing technologies that have this functionality, but they come with a lot of other functionality. I’m trying to create an input that isolates this functionality.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:44:30+00:00Added an answer on June 13, 2026 at 7:44 am

    Here’s a start that you can tweak yourself: jsFiddle.

    I did it in two replacements, first to add the <ul></ul>, and next to add the <li></li>s. (Doing it in one step would’ve been easier if JavaScript supported lookbehind assertions; without them it’s still possible but messy.)

        val = val.replace(/((?:(?:^|[\n\r]+)[\t ]*-[\t ]*[^\n\r]*)+)/g, "\n<ul>\n$1\n</ul>");
        val = val.replace(/[\n\r]+[\t ]*-[\t ]*([^\n\r]*)/g, "\n  <li>$1</li>");
    

    I made some assumptions while constructing this, which you may have to undo:

    1. Treat series of newlines as one newline.
    2. Remove spaces and tabs before and after the -.

    The following input,

    hello, world.
    - two
    - things
    hi, again.
    - three
     -more 
    -things
    

    creates the following output:

    hello, world.
    <ul>
      <li>two</li>
      <li>things</li>
    </ul>
    hi, again.
    <ul>
      <li>three</li>
      <li>more </li>
      <li>things</li>
    </ul>
    

    EXPLANATION

    The first regex simply identifies a set of list items.

    (                   Captured group ($1).
    
        (?:             Group (one list item). -------------------+
                                                                  |
            (?:         Group (for alternation). ---------+       |
                                                          |       |
                ^       Start-of-string                   |       |
                                                          |       |
                |           OR                      <-----+       |
                                                                  |
                [\n\r]+     one or more newlines.                 |
                                                                  |
            )                                                     |
                                                                  |
            [\t ]*      (Ignore tabs and spaces.)                 |
            -           (Dash.)                                   |
            [\t ]*      (Ignore tabs and spaces.)                 |
                                                                  |
            [^\n\r]*    List item text (everything but newlines). |
                                                                  |
        )                                                         |
        +               One or more list items. <-----------------+
    
    )
    

    This set of list items, captured in $1, is wrapped in <ul></ul> tags:

    "\n<ul>\n$1\n</ul>"
    

    The second regex wraps each list item in <li></li> tags, and is very similar to the first, so it may be more useful to show what has changed:

    first regex  : /((?:(?:^|[\n\r]+)[\t ]*-[\t ]* [^\n\r]* )+)/g
    differences  :  xxxxxxxxx       x             (        )xxx
    second regex : /         [\n\r]+ [\t ]*-[\t ]*([^\n\r]*)   /g
    

    In words,

    1. we no longer care about the set of list items, only each list item, so we can drop the non-capturable group that was used for quantifying, (?:...)+,

    2. after the first regex substitution (which prepends a \n<ul>\n), it should be impossible for a list item to begin at the start of the string, so we can drop the alternation, (?:^|...),

    3. however we are now interested in capturing the list item text, so we add a capturing group, (...).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have an array which has BIG numbers and small numbers in it. I
I have a text area in my form which accepts all possible characters from
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.