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:
- range.surroundcontents() (doing that, selections)
- string methods: trim() (doing opposite white spaces), split/join(), anchor/link()
- deprecated string methods: big(), blink(), bold(), fixed(), fontcolor(), fontsize(), italics(), small(), strike(), sub/sup()
edit: such questions not stupid creators of javascript.
who more stupid: creators of javascript or you?
use cases
i wish write like:
div.innerhtml = rawstr.split("\n\n").join("", "<p>", "</p>");
newstr = oldstr.split(/\s+“|”\s+/g).join(" ", "\"", "\"");
var str = "joan – despite curly hair – loved.";
return str.split(/\s+–/s+/g).join(" ", "(", ")");
var str = "joan (despite curly hair) loved.";
return str.split(/\s+\(|\)/s+/g).join(" – ");
← oh, wait... can one.
...instead of:
div.innerhtml = rawstr.split("\n\n").map(str => "<p>" + str + "</p>")).join("");
newstr = oldstr.split(/\s+“|”\s+/g).map(str => "\"" + str + "\"")).join(" ");
- a)
var str = "joan – despite curly hair – loved.";
b)
var p = str.split(/\s+–/s+/g);
return p[0] + " " + "(" + p[1] + ")" + " " + p[2];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
Post a Comment