0

How to move the colors and size beside of "Color and Size" using regex? This is my regex: https://regex101.com/r/fB3jK9/1

The result should be like this:

My sample input is:

Color and Size - Dark Blue|Small
    Dark Blue|Medium
    Dark Blue|Large
    Dark Blue|X-Large
Color and Size - Black|Small
    Black|Medium
    Black|Large
    Black|X-Large
    Ocean|Small
    Ocean|Medium
    Ocean|Large
    Ocean|X-Large
Color and Size - Black|Small
    Black|Medium
    Black|Large
    Black|X-Large
    Black|2X-Large
    Stone|Small
    Stone|Medium
    Stone|Large
    Stone|X-Large
    Stone|2X-Large

So far all I've been able to come up with to use is:

\n\t

But I need the output of:

Color and Size - Dark Blue|Small, Dark Blue|Medium, Dark Blue|Large, Dark Blue|X-Large



Color and Size - Black|Small, Black|Medium, Black|Large, Black|X-Large, Ocean|Small, Ocean|Medium, Ocean|Large, Ocean|X-Large






Color and Size - Black|Small, Black|Medium, Black|Large, Black|X-Large, Black|2X-Large, Stone|Small, Stone|Medium, Stone|Large, Stone|X-Large, Stone|2X-Large








// end output
9
  • yes so where's the question ? Commented Mar 30, 2015 at 6:16
  • You shouldn't do this via regex, but it's probably possible to HARDCODE it to work right. I'll write up a solution in python. Commented Mar 30, 2015 at 6:17
  • @AdamSmith, Im using jquery, hope you can help me Commented Mar 30, 2015 at 6:18
  • @NewtoJava if you're using jQuery, that means you have access to Javascript. I haven't used js in about a decade, so I can't, but I'll retag your question as such. Edit your question so it's obvious Commented Mar 30, 2015 at 6:19
  • 2
    For completeness sake, edit your question and add 1) the input string 2) the regex you used and 3) the current output you get. Commented Mar 30, 2015 at 6:22

1 Answer 1

1

I suggest getting the 'blocks' together and the running the replacement inside a function, for example like this:

// Getting a block
var regex = /Color and Size[\s\S]*?(?=Color and Size|$)/gi;

// Taken from http://stackoverflow.com/q/29339480/1578604 to repeat string
String.prototype.repeat = function( num ) {
    return new Array( num + 1 ).join( this );
}

var results = text.replace(regex, function(m) {
    // Get number of line breaks
    var lb = m.split("\n").length - 1;
    // Now remove newlines and tabs, then add the linebreaks at the end
    return m.replace(/\n\t/g, ", ") + "\n".repeat(lb);
});

jsfiddle

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

2 Comments

Thanks @Jerry can you convert into jquery?
@NewtoJquery Sorry, I don't know JQuery at all :( From my understanding though, there shouldn't be anything to convert as there's just a string, a function and a replace. You should be able to insert it among your code I think.

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.