1

Is it possible to join array elements with a regex? If so, how do I achieve these requirements?

  • Every element should be joined with a spacing character unless it is an empty element.
  • Empty array elements should be joined with a new line character (\n).

That means that this:

["Hello, this is a sentence.", "This is another sentence.", "", "", "Then, there are 2 new lines.","","Then just one new line."]

Should be converted with .join to this:

Hello, this is a sentence. This is another sentence.

Then, there are 2 new lines.
Then just one new line.

3 Answers 3

1
var string = "";

for(var index = 0; index < elements.length; index++) {
    var lastElement = elements[index -1];
    string += elements[index] !== "" ? (lastElement && lastElement !== ""? " " + elements[index] : elements[index]) : "\n";
}
console.log(string);

DEMO

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

5 Comments

almost worked fine. i still don't know why is it adding a space character at the beginning of all sentences.
@andufo Sorry, I didn't see it. Fixed.
still has some issues when the sentences are joined without newlines between them, but you gave me the main idea. i'll try to take it from there. thanks!
i had to accept @kevin-b 's answer, it covered all the spacing issues between sentences as well.
worked fine this time, with less code. i just had to add a $.trim(string) to get rid of the last space character. thanks!
1

Loop through the array replacing empty elements with <br /> or \n depending on where you are using the string, then join it on "".

for (var i = 0; i < myArr.length; i++) {
    myArr[i] = myArr[i] === "" ? "\n" : myArr[i];
}
var myStr = myArr.join("");

Edit: here's a full demo with your additional requirements: http://jsfiddle.net/auAAH/

var myArr = ["Hello, this is a sentence.", "This is another sentence.", "", "", "Then, there are 2 new lines.", "", "Then just one new line."];
for (var i = 0; i < myArr.length; i++) {
    if (myArr[i] === "") {
        myArr[i] = "\n";
        if (i !== 0 && myArr[i - 1] !== "\n") {
            myArr[i - 1] = myArr[i - 1].replace(/ $/, "");
        }
    }
    else if (i < myArr.length-1) {
     myArr[i] = myArr[i] + " ";   
    }
}
var myStr = myArr.join("");
document.getElementsByTagName("textarea")[0].value = myStr​;​

2 Comments

I'm using it on a textarea. I have to join them with a space character, else the sentences will have no spacing between them. I also don't want unnecessary spaces in newlines.
Either way all of the changes will need to be done in a loop of some kind.
0

You should first use a match/replace loop to modify the elements according to your rules, and then you should .join the resulting array.

Comments

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.