# Reg Expression Help Please?

**URL:** <https://www.ruby-forum.com/t/reg-expression-help-please/166810>\
**Category:** Rails\
**Created:** [April 29, 2009, 8:10pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810 "2009-04-29T20:10:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:10pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/1 "2009-04-29T20:10:10Z")

</div>

Hi all

I’m having a bit of trouble trying to acheive something, maybe someone  
can help?

I have a model Article, which has an attribute ‘body’

The body is a text column in which people can add text and HTML.

I’d like to edit certain properties of some of the HTML tags, for  
example, converting all spaces inside ` ` tags to &nbsp;

I presume using a reg expression is the best way to achieve this, I’m  
just not sure of how to word an expression to scan for only characters  
within the html tags.

any ideas?

Ta

Gavin

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:14pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/2 "2009-04-29T20:14:52Z")

</div>

You don’t want to fall into the rat hole of parsing HTML with  
regexes. You need a parsing library like hpricot or similar.

[http://wiki.github.com/why/hpricot](http://wiki.github.com/why/hpricot)

good luck!  
Tim

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/3 "2009-04-29T20:27:48Z")

</div>

Use a CSS rule to style it?

code { white-space: pre; }

or:

code { white-space: nowrap; }

You’re just asking for a headache if you try to match paired and  
possibly nested HTML tags with a Regexp. (Not that it can’t be done,  
but it gets ugly fast and you need a very capable regular expression  
engine like Oniguruma from Ruby1.9)

-Rob

[http://www.w3.org/TR/CSS2/text.html#white-space-prop](http://www.w3.org/TR/CSS2/text.html#white-space-prop)

On Apr 29, 2009, at 2:09 PM, Gavin wrote:

> I’d like to edit certain properties of some of the HTML tags, for  
> Gavin
> 
> >

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/4 "2009-04-29T20:29:33Z")

</div>

> The body is a text column in which people can add text and HTML.

If this is the use case, then you want to protect what the user  
enters.

Using RedCloth or other markup library might do a better job of what  
you are trying to achieve.

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:29pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/5 "2009-04-29T20:29:55Z")

</div>

> I’d like to edit certain properties of some of the HTML tags, for  
> example, converting all spaces inside ` ` tags to &nbsp;

Take a look at CGI#escape and CGI#escapeElement

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/6 "2009-04-29T20:32:30Z")

</div>

I was planning on editing the text blob before it’s saved to the  
database.

So a blob like:  
“This is a big blob of text  
`This is the code part`  
This is another line”

would be converted to:  
“This is a big blob of text  
`This is the code part`  
This is another line”

And then called back as \<%= @article.body %\> in the view.

I thought that was a simple option.

Would hpricot be appropriate here?

I should also add that I plan on having a safe-list of tags, so any  
potentially harmful tags like would be removed

Does that clarify at all?

Thanks

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/7 "2009-04-29T20:34:51Z")

</div>

I had planned on formatting the code tags with CSS as you suggested  
Rob but I also need to wrap specific words in spans to specify their  
colour

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [April 29, 2009, 8:45pm UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/8 "2009-04-29T20:45:58Z")

</div>

Actually…

Just found this =\> [http://coderay.rubychan.de/](http://coderay.rubychan.de/)

looks perfect for my needs

Thanks for your suggestions guys

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [June 5, 2009, 11:42am UTC](https://www.ruby-forum.com/t/reg-expression-help-please/166810/9 "2009-06-05T11:42:22Z")

</div>

Hi all

Managed to solve this issue with CodeRay

The site is now up and running - here is a quick tutorial on how I did  
it incase anybody else wants to do the same:

> **[Styling Your Blog's Code To Look Like Your Favorite Text Editor With CodeRay](https://handyrailstips.com/tips/5-styling-your-blog-s-code-to-look-like-your-favorite-text-editor-with-coderay/)**
>
> For those of you out there with your own blog who want to style your snippets of code to look like your favourite text editor, here is a simplified version of the method I’ve used here on Handy Rai...

🙂
