usted no necesidad jQuery para esta tarea simple. String.prototype.replace
y regex deberían hacer el truco.
acabo de hacer algunas funciones de utilidad simples, que envuelve letras, palabras y líneas:
/**
* 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>");
}
El uso es bastante simple. Solo pase la cadena para envolverla como primer argumento. Si necesita un marcado personalizado, páselo como segundo argumento, mientras que $&
se reemplaza por cada carácter/palabra/línea.
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>"
Es posible que desee examinar el complemento [Lettering] (http://letteringjs.com/). – Pointy
¿Cuál es la salida deseada? – SpoonNZ
Intenta mejorar tu pregunta dando un ejemplo de lo que quieres. Un mejor nombre de pregunta parece ser '¿Cómo ajustar ** cada palabra ** de un elemento en una etiqueta de intervalo?' – Richard