An elegant way to surround a string in Javascript -


are there no better ways surround string html tag (or brackets, or quotation marks, or whitespace) mundane concatenation?

inbefore “stupid question”

very similar problems notable enough deserve own api functions:

edit: such questions not stupid creators of javascript.

who more stupid: creators of javascript or you?

use cases

i wish write like:

  1. div.innerhtml = rawstr.split("\n\n").join("", "<p>", "</p>");
  2. newstr = oldstr.split(/\s+“|”\s+/g).join(" ", "\"", "\"");
  3. var str = "joan – despite curly hair – loved.";
    return str.split(/\s+–/s+/g).join(" ", "(", ")");
  4. var str = "joan (despite curly hair) loved.";
    return str.split(/\s+\(|\)/s+/g).join(" – "); ← oh, wait... can 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 curly hair – loved.";
    var p = str.split(/\s+–/s+/g);
    return p[0] + " " + "(" + p[1] + ")" + " " + p[2];
    b)
    var str = "joan – despite curly hair – loved.";
    var p = str.split(/\s+–/s+/g);
    p[1] = "(" + p[1] + ")";
    return p.join(" ");

when can this, life complete.

this easier 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]}` 

Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -