になります。 String.prototype.replace
と正規表現は、そのトリックを行う必要があります。
手紙をラップ私はいくつかの簡単なユーティリティ関数を作っ
、単語や行:
/**
* Wraps a string around each character/letter
*
* @param {string} str The string to transform
* @param {string} tmpl Template that gets interpolated
* @returns {string} The given input as splitted by chars/letters
*/
function wrapChars(str, tmpl) {
return str.replace(/\w/g, tmpl || "<span>$&</span>");
}
/**
* Wraps a string around each word
*
* @param {string} str The string to transform
* @param {string} tmpl Template that gets interpolated
* @returns {string} The given input splitted by words
*/
function wrapWords(str, tmpl) {
return str.replace(/\w+/g, tmpl || "<span>$&</span>");
}
/**
* Wraps a string around each line
*
* @param {string} str The string to transform
* @param {string} tmpl Template that gets interpolated
* @returns {string} The given input splitted by lines
*/
function wrapLines(str, tmpl) {
return str.replace(/.+$/gm, tmpl || "<span>$&</span>");
}
使用量は非常に単純です。最初の引数としてラップするために文字列を渡すだけです。カスタムマークアップが必要な場合は、2番目の引数として渡します。$&
は、各char/word/lineに置き換えられます。
var str = "Foo isn't equal\nto bar.";
wrapChars(str); // => "<span>F</span><span>o</span><span>o</span> <span>i</span><span>s</span><span>n</span>'<span>t</span> <span>e</span><span>q</span><span>u</span><span>a</span><span>l</span> <span>t</span><span>o</span> <span>b</span><span>a</span><span>r</span>."
wrapWords(str); // => "<span>Foo</span> <span>isn</span>'<span>t</span> <span>equal</span> <span>to</span> <span>bar</span>."
wrapLines(str); // => "<span>Foo isn't equal</span> <span>to bar.</span>"
[Lettering](http://letteringjs.com/)プラグインを調べるとよいでしょう。 – Pointy
希望する出力は何ですか? – SpoonNZ
あなたが望むものの例を挙げて質問を改善してください。より良い質問名は、「スパンタグ内の要素の各単語**をどのように包むか」のようです。 – Richard