views:

66

answers:

3

if the h2 has the word ply i want to add a break.

the titles are dynamically called, the site is html so i can't use php on anything.

heres the h2

11/400-5 4 PLY EXPERIMENTAL TIRE AIRCRAFT TIRE

want it to look like this

11/400-5 4 PLY
EXPERIMENTAL TIRE AIRCRAFT TIRE

so any title that has the word PLY in it will add a break right after the word.

probably pretty easy for all yous out there =)

+4  A: 

Example: http://jsfiddle.net/DbJ2V/

$('h2:contains(" PLY ")').html( function(i,htm) {
     return htm.replace(" PLY ", " PLY<br/>");
});
patrick dw
what if you got tabs, not spaces, at either side?
Adriano Varoli Piazza
i can just tweak the contents i can see how it works. thanks this works great
Alex
+1  A: 

Let's try answer instead of commenting:

$("h2:contains('PLY')").each(function() {
    $(this).html($(this).html().replace(/(\WPLY\W)/g, "$1<br />"));
});

First we look for any h2 with the text 'PLY', then replace it, if it is indeed not part of a word (for example, -PLY would match, _PLY wouldn't, blame regexes for what is part of a word and what is not), with the same content plus a <br />.

Adriano Varoli Piazza
`\b` would be good too, instead of `\W`, depends on choice. I really haven't put much thought to which would be best in this case (and indeed, a simple `\s` or just spaces could be enough).
Adriano Varoli Piazza
A: 

Try:

$(document).ready(function() {
    $("#content h2:contains(' PLY ')").each(function(k,v) {
        t = $(v).text();
        $(v).html(t.replace(" PLY ", " PLY<br/>"));
    }); 
});
GeekyJohn