1

I am trying to change the HTML tag and remove the class/style attributes after the tag. I already know how to do this if I create the code before hand and replace, now I want to know how to find the tags on an already loaded page and replace them with my js.

var s = "<h2 class=\"title\" style=\"font-color: red;\">Blog Post</h2>";
s = s.replace("<h2 class=\"title\" style=\"font-color: red;\">","<p>");
s = s.replace(/<\/h2>/g, "</p>");

Start with <h2 class="title" style="font-color: red;">Blog Post</h2>

End with <p>Blog Post</p>

So the question is how can I create the var s with existing HTML? How do I find h2.title on a page and give it to var s?

edit I have no javascript experience except for this script which I found and tweaked. Please explain how I can grab text from an existing document, and make it my var for s.replace to manipulate.

  • Do you just want to change the HTML itself? From `

    Blog Post

    ` to `

    Blog Post

    `
    – Ruan Mendes Nov 28 '12 at 00:42
  • you shouldn't be using regex to parse HTML, grab the tag/class name and replace it accordingly with DOM methods. – rlemon Nov 28 '12 at 00:45
  • @juan-mendes Yes, I'm trying to manipulative the code exactly like the start and end example in my post. The javascript does this already, my code does exactly what I want, but only if I create the var s, I want var s to be dynamic, I want var s to find `

    blog post 1

    ` and then run `s.replace`.
    –  Nov 28 '12 at 00:50
  • @RzK It's really hard to tell what you mean by `want var s to be dynamic, I want var s to find

    blog post 1

    and then run s.replace` But two things are for sure: your regexp is going to be very brittle; also you can't just expect a call to `String.replace()` to update the DOM, you'd have to set the `innerHTML` of a parent node (assuming it was the only child)
    – Ruan Mendes Nov 28 '12 at 00:54
  • @Rzk You are going the wrong way. Why do you keep updating your question to ask us to teach you do something in a way that is not advisable? `h2.title` is not a string, you can't just call `.replace()` on it – Ruan Mendes Nov 28 '12 at 00:56
  • @Rzk See my answer for an example of how you can do what you're trying – Ruan Mendes Nov 28 '12 at 01:02

2 Answers2

2

Don't try to do it with regular expressions, you should use DOM manipulation to move the text node in question to a p tag you create. Here's some code that will do what you need.

http://jsfiddle.net/jWRh5/

// Find the h2
var h2 = document.querySelector("h2");
// Create the p element you need
var p = document.createElement("p");
// Move the text node into the p element
p.appendChild(h2.firstChild);
// Insert the p element before the h2
h2.parentNode.insertBefore(p, h2);
// Get rid of the h2
h2.parentNode.removeChild(h2);

If you want to go against what every one else suggests, here's a way to use RegExp to achieve what you need http://jsfiddle.net/jWRh5/1/

It uses a not well supported feature, outerHTML (it does work in most recent versions of the major browsers)

var h2 = document.querySelector("h2.title");
var s = h2.outerHTML;
s = s.replace("<h2 class=\"title\" style=\"font-color: red;\">","<p>");
s = s.replace(/<\/h2>/g, "</p>");
h2.outerHTML = s;

Here's how to do it for all h2.titles on your page (not using the RegExp way since that's a really poor way, but if you're really set on using it, you can use it as a guide)

var h2s = document.querySelectorAll("h2.title");
// Loop backwards since we're modifying the list
for (var i = h2s.length -1 ; i >=0; i--) {
    var h2 = h2s[i];
    var p = document.createElement("p");
    p.appendChild(h2.firstChild);
    h2.parentNode.insertBefore(p, h2);
    h2.parentNode.removeChild(h2);
} 
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
  • Awesome, worked perfectly how would I be able to loop the changes for all the h2 elements? Thank you –  Nov 28 '12 at 01:08
  • @Rzk Here's a clue, use `document.querySelectorAll` which returns all the h2 elements. But please don't use `RegExp`s, that's shooting yourself in the foot, use the first example (and accept the answer). – Ruan Mendes Nov 28 '12 at 01:12
  • Thank you for your help, but document.querySelectorAll returns none of the elements when using RegExp or DOM manipulation. –  Nov 28 '12 at 01:24
  • @Rzk What you said makes no sense. – Ruan Mendes Nov 28 '12 at 16:38
  • I was trying to select all h2 elements on the page, using document.querySelectorAll. Using document.querySelectorAll in both of the answers you gave me, but it doesn't work(instead of selecting all the H2 elements and manipulating them, none of the elements are changed).I just made my script sloppy and decided to repeat the entire script 10 times so it manipulates all the posts on a page. So no worries it's a personal userscript no need to worry about the inefficany of the userscript. Thanks agai –  Nov 29 '12 at 02:39
  • I haven't been on Stack for a while, so thank you for updating the code. Have a good day. –  Dec 10 '12 at 04:55
1

For this kind of thing, jQuery pays dividends, real quick-like.

The code to do what you want is just:

$("h2").replaceWith ( function () {
    return '<p>' + this.textContent + '</p>';
} );


Or, if you want to be more specific:

$("h2.title").replaceWith ( function () {
    return '<p>' + this.textContent + '</p>';
} );


Note that that code fixes all of the <h2> elements (first block), or just all of the <h2> elements with class title (second block).


For information on how to include jQuery in your userscript, see this answer for a cross-browser approach.

Documentation on the .replaceWith() function.

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • You are suggesting add a library for this one feature? I think you need a clearer disclaimer since OP did not ask for jQuery – Ruan Mendes Nov 28 '12 at 17:52