Regex to get innerHTML of <code></code> Block

982 views
Skip to first unread message

Patrick Heppler

unread,
Mar 7, 2014, 5:27:34 AM3/7/14
to ra...@googlegroups.com
Hi,
I'm trying to get innerHTML from <code></code> blocks stored in a database. At the moment my regex only matches oneliners. Regex: <code.*?>(.*?)<\/code>

<code>Do this and then that</code>
Match: "Do this and then that"

<code>
Do this 
and 
then that
</code> 
No Match

Any Idea how to get the innerHTML including new lines?

Jonas Hauß

unread,
Mar 7, 2014, 11:15:16 AM3/7/14
to ra...@googlegroups.com
Hey Patrick,

Try this:

<cfsavecontent variable="wordsToMatch">

<code>
    Do this
    and
    then that
</code>
</cfsavecontent>

<cfdump var="#wordsToMatch#">
<cfdump var="#reReplace(wordsToMatch, "<code>(.+?)(?s)<\/code>", "\1")#">

Here is the explanation:

http://regex101.com/r/eC7bZ2

In my case the s modifier did the trick ;)

I hope this helps you.


Jonas

Gerald Guido

unread,
Mar 7, 2014, 3:37:23 PM3/7/14
to railo
>>http://regex101.com

What an awesome resource! Thanx for sharing that. 

G!

--
Gerald Guido


--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/railo/05e9ca9f-31a1-4a50-9b9c-01ac8005a6ff%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Chris Blackwell

unread,
Mar 7, 2014, 4:15:35 PM3/7/14
to railo
I love regex, but just for fun, heres an alternative.
download my jsoup custom tag and then you can use jquery-ish syntax to get the text.

<cfimport taglib="/cfjsoup" prefix="dm" />

<dm:jsoup variable="myhtml">
<code>
Do this 
and 
then that
</code> 
No Match
</dm:jsoup>

<cfscript>
mycode = $('code')[1].textnodes()[1].getWholeText();
echo(mycode);
</cfscript>

if you didn't want line breaks you could do this with just ${'code').text()

Cheers, Chris


--
Did you find this reply useful? Help the Railo community and add it to the Railo Server wiki at https://github.com/getrailo/railo/wiki
---
You received this message because you are subscribed to the Google Groups "Railo" group.

Jonas Hauß

unread,
Mar 7, 2014, 6:15:21 PM3/7/14
to ra...@googlegroups.com
No problem, Gerald.

This page is awesome and really helped me out working with regular expressions!

I hope it will be helpful to you too. :)

Patrick Heppler

unread,
Mar 10, 2014, 4:23:51 AM3/10/14
to ra...@googlegroups.com
Thanks, this one works!
Reply all
Reply to author
Forward
0 new messages