0

Given a Markdown source text:

- sometext

- sometext
followingText



- sometext

- sometext
- sometext

I want to replace double newlines \n\n to \nWhiteSpace\n among the List range;

so with $1 technique of JS, I did

(- [\s\S]+?)(?:\n\n(?=\n))|(- [\s\S]+?)(?:\n\n(?=- )) http://regex101.com/r/hO7vT9

The blue selection is the target where almost working, except the very first double newlines are failed to be selected.

This is because (- [\s\S]+?)(?:\n\n(?=\n)) matches first and the (- [\s\S]+?)(?:\n\n(?=- )) is not evaluated.

Sure, I can revert the order (- [\s\S]+?)(?:\n\n(?=- ))|(- [\s\S]+?)(?:\n\n(?=\n)), then now

http://regex101.com/r/tR8sN4

Now, the first selection works, but the second selection failes.(compare to the first version, the intended result for the second selection).

Is there any work-around for this?

Of course, you may suggest separate the regex, and replace twice; well I did, and the result is messy, so I would like achieve this in a single regex. The language is JS.

Community
  • 1
  • 1
  • I am sorry if I completely misunderstood your question, but is [**this**](http://jsfiddle.net/hari_shanx/xLhqR/) what you are looking for? – Harry Aug 10 '13 at 04:17
  • Thanks, Harry. Well, no. For some technical reason, the double new line must be within the specific list structure context. –  Aug 10 '13 at 04:44
  • Are you parsing markdown? You probably shouldn't be using regular expressions for this. Have you considered parsing line-by-line and using regular expressions on each line separately? – Brigand Aug 10 '13 at 05:11
  • That is fine. I have updated the [fiddle](http://jsfiddle.net/hari_shanx/xLhqR/) now. My last attempt, check and if this is also not what you want, I quit :D – Harry Aug 10 '13 at 05:11

1 Answers1

0

Posting my comment as an answer.

Try the below. Sorry, if this is still not what you want.

regex = new RegExp(/(- [\s\S]+?)(?:(\n){2,}(?=\n|-))/g);
inputString = '- sometext\n\n- sometext\nfollowingText\n\n\n\n- sometext\n\n- sometext\n- sometext';
outputString = inputString.replace(regex,'$1\nWhiteSpace\n');
console.log(outputString);
Harry
  • 87,580
  • 25
  • 202
  • 214
  • Thanks, Harry. actually, I gave up what I was doing. In fact, I tried to hack marks for my own extension, but too complicated, so I shall start from scrach by my own. anyway Check +1 for your kind contribution. Appreciated –  Aug 10 '13 at 06:22