简体   繁体   中英

Getting innerHTML of PRE tag with Regular Expression (Javascript)

I have this HTML string.

some text <pre>01     02     bb      67
02     g2     cc      67<br />
<b>03     32     dd      67</b>
04     q2     ee      67</pre> some text

I need a javascript regex to get the text/html inside the PRE tag and manipulate it. It's pure text, no DOM manipulation here.

Ps Sorry for my english.

Here's how you can get the contents of the pre tag using javascript's match() method:

var preContents = myHTMLString.match(/<pre>.*?<\/pre>/ims).substr(5, -6);

That searches for a pre tag, the contents, and the closing pre tag, then removes the opening and closing pre tags, and stores the contents in the preContents variable.

To manipulate it, you could then edit preContents to suit your needs and use javascript's replace() method to change the contents:

myHTMLString = myHTMLString.replace(/<pre>.*?<\/pre>/ims, '<pre>' + manipulatedPreContents + '</pre>');

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