-2

I'm having difficulties in executing what I want to achieve, it may just be sleep deprivation but it's more likely that regex is not my strong suit, I just can't quite get my head around it, but hopefully someone can give me a hand here.

I have the following string:

<span class="comment"><!--Some string\nsome other string\nsome more string--></span>

I need to format it so that it looks like this:

<span class="comment"><!--Some string</span>\n<span class="comment">some other string</span>\n<span class="comment">some more string--></span>

Now this would be really easy if this was the only string as I could do something like this:

/</span>\n<span class="comment">/gi

However the formatting should only happen if the corresponding open </span> has the class "comment" the other issue is that the open span tag with the class comment is not necessarily the first word of the string, there could be a string in-front of it and there could be infinitely many \n within the span...e.g. another variation could look like this:

&lt;<span class="tag">string1\nstring2</span>&gt;<span class="comment"><!--string\nanother random string--></span>

No formatting should take place within the span with class tag, however formatting should take place in the span with class comment.

This is rather challenging for me to get my head around, the closest I have gotten is the following:

regex:

/<span class="comment">([^\<\/]*)\n/gi

replacement:

<span class="comment">$1</span>\n<span class="comment">

This gets close, as it formats the last line and first line within the span with class comment, but not the lines in between.

There may be javascript solutions to do this, but if at all possible I would prefer using regex.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lennart
  • 1,560
  • 5
  • 20
  • 38
  • 1
    Are you just trying to replace `\n` with ``? You don't need regex for that. – Evan Davis May 30 '13 at 18:26
  • 2
    @Mathletics, it looks like he wants `\n`, but other than that detail you seem to be right! – jamesplease May 30 '13 at 18:28
  • @sircodesalot I don't think that applies; this is actually subbing tags INTO a string, not parsing HTML. – Evan Davis May 30 '13 at 18:30
  • But only if the `\n` is in a span with `class="comment"` if I am reading this correctly – HBP May 30 '13 at 18:34
  • @HBP exactly, but in regex, as there are js solutions to this, but regex would be best for my needs – Lennart May 30 '13 at 18:35
  • 1
    I am just completely confused by what OP thinks regex is/does. – Evan Davis May 30 '13 at 18:37
  • @Mathletics Im using it as search and replace, what I am working on is a web based code editor which will support a variety of different languages, i currently have planned that each language will have an xml file with the necessary regex expressions and replacements... you can have a look at the project at https://github.com/motorlatitude/coder if you wish – Lennart May 30 '13 at 18:40
  • It seems that a loop would solve your issue, and using a lazy `*` as well. So that you run the replace once for each string. – Jerry May 30 '13 at 18:42
  • 1
    What do you mean "_I would prefer using regex_"? Regular expressions don't do text transforms; it's just a way of matching patterns. You still need to use JavaScript (or some language) to handle the replace, and in fact you do not require regex at all for the matching based on the scenario you've described. – Evan Davis May 30 '13 at 18:43

1 Answers1

0

Here you go,

 str.replace (/<span\s+class="comment">.*<\/span>/ig, function (m) {
     return m.replace (/\n/g, '</span>\n<span class="comment">');
   });

Extract the span and its contents, then replace all the \n in that part.

HBP
  • 15,685
  • 6
  • 28
  • 34
  • Thank you, I managed to get your solution working within my project and thank you for taking your time to help, considering this question was rather difficult to explain! – Lennart May 30 '13 at 19:00