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.

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.
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.)I made some assumptions while constructing this, which you may have to undo:
-.The following input,
creates the following output:
EXPLANATION
The first regex simply identifies a set of list items.
This set of list items, captured in
$1, is wrapped in<ul></ul>tags: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:In words,
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,
(?:...)+,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,(?:^|...),however we are now interested in capturing the list item text, so we add a capturing group,
(...).