简体   繁体   中英

Replace HTML between two tags

What I want to do is replace what is in between two HTML tags.

I'm using this as a reference but I'm still encountering problems: REFERENCE

This is what I've tried:

el.getValue().replace(/<form.+<\/form>/, "<div></div>");

I need to replace all my form tags dynamically.

If you use jQuery, just retrieve the parent element of what you'd like to be replaced, and replace the content with the .html() function.

Ex:

var formParentElement = $('#formParentElement');
formParentElement.html("<div>my new content</div>");

If you don't use jQuery:

var formParentElement = document.getElementById("formParentElement");
formParentElement.innerHTML = "<div>my new content</div>";

The example assumes the parent element of your form has an ID with value "formParentElement".

Yeah. I found a solution.

el.getValue().replace(/<form[\s\S]*?<\/form>/, "<div></div>");

Explanation by @[James G]: [\\s\\S]*? means [any character including space and line breaks]any number of times, and the ? makes the asterisk "not greedy," so it will stop (more quickly) when it finds </form> .

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM