-15

Are there no better ways to surround a string with a HTML tag (or brackets, or quotation marks, or whitespace) than mundane concatenation?

Inbefore “stupid question”

Very similar problems are notable enough to deserve own API functions:

Edit: Such questions are clearly not stupid for creators of Javascript.

Who is more likely stupid: creators of Javascript or you?

Use cases

I wish I could write something like:

  1. div.innerHTML = rawstr.split("\n\n").join("", "<p>", "</p>");
  2. newstr = oldstr.split(/\s+“|”\s+/g).join(" ", "\"", "\"");
  3. var str = "Joan – despite her curly hair – is loved.";
    return str.split(/\s+–/s+/g).join(" ", "(", ")");
  4. var str = "Joan (despite her curly hair) is loved.";
    return str.split(/\s+\(|\)/s+/g).join(" – "); ← Oh, wait... I can already do this one.

...instead of:

  1. div.innerHTML = rawstr.split("\n\n").map(str => "<p>" + str + "</p>")).join("");
  2. newstr = oldstr.split(/\s+“|”\s+/g).map(str => "\"" + str + "\"")).join(" ");
  3. a)
    var str = "Joan – despite her curly hair – is loved.";
    var p = str.split(/\s+–/s+/g);
    return p[0] + " " + "(" + p[1] + ")" + " " + p[2];
    b)
    var str = "Joan – despite her curly hair – is loved.";
    var p = str.split(/\s+–/s+/g);
    p[1] = "(" + p[1] + ")";
    return p.join(" ");

When I can do this, my life will be complete.

Community
  • 1
  • 1
7vujy0f0hy
  • 8,741
  • 1
  • 28
  • 33
  • 1
    ES6 template literals makes your life easier. – Tatsuyuki Ishi Apr 14 '17 at 13:08
  • 4
    `Who is more likely stupid: creators of Javascript or you?` what the hell is this about - I'd say the answer is "you" – Jaromanda X Apr 14 '17 at 13:09
  • @JaromandaX: “Creators of Javascript or me” is a false dichotomy because creators of Javascript think like me. I’ve shown it right there but you’ve simply filtered it out of your mind. The correct dichotomy is “creators of Javascript or you”. – 7vujy0f0hy Apr 14 '17 at 13:21
  • 2
    @Shilly Shouldn't `.join('

    ')` be `.join('

    ')`?

    – Dan D. Apr 14 '17 at 13:33
  • @Shilly: Your **snippet 1** still involves ugly concatenation. (Where I define “ugly” as “happening inside rather than between conceptual text units”.) Your **snippet 2** is faulty: it will make one giant paragraph instead of separate paragraphs for each `\n\n` section. Your **snippet 3** is correct but unrelated to the subject of surrounding a string. – 7vujy0f0hy Apr 14 '17 at 13:44

1 Answers1

-1

This is much easier with ES6 template literals:

div.innerHTML = rawstr.split("\n\n").map(str => `<p>${str}</p>`)).join("");
newstr = oldstr.split(/\s+“|”\s+/g).map(str => `"${str}"`)).join(" ");
`${p[0]} (${p[1]}) ${p[2]}`
Tatsuyuki Ishi
  • 3,883
  • 3
  • 29
  • 41
  • Most likely the only solution I will ever find. I had known about template literals but I forgot to mention them in the question. I still think that it should be asked. – 7vujy0f0hy Apr 14 '17 at 14:06
  • If only templates could be applied to arrays... they would be perfect: `div.innerHTML = \`

    ${...rawstr.split("\n\n")}

    \`;` ☺.
    – 7vujy0f0hy Apr 14 '17 at 14:34
  • `div.innerHTML = \`

    ${rawstr.split("\n\n").join("

    \n\n

    ")}

    \`;`
    – 7vujy0f0hy Apr 14 '17 at 15:05