<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Irish &#124; Ruby, Rails, Javascript Developer &#124; Phoenix, AZ &#187; Ruby on Rails</title>
	<atom:link href="http://www.christopherirish.com/tag/ruby-on-rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christopherirish.com</link>
	<description>Ruby, Rails, Javascript, all things web development related</description>
	<lastBuildDate>Mon, 19 Jan 2026 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Splitting Rails DOM ids with RegEx Look Aheads</title>
		<link>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/</link>
		<comments>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 08:27:02 +0000</pubDate>
		<dc:creator>Irish</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Regular Expressions]]></category>
		<category><![CDATA[Ruby on Rails]]></category>

		<guid isPermaLink="false">http://www.christopherirish.com/?p=530</guid>
		<description><![CDATA[In my Rails erb views I make good use of the dom_id view helper method. It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id. So for example if I had a Post with id 1, I could [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>In my Rails erb views I make good use of the dom_id view helper method.  It basically introspects the object you pass to it and creates a unique id to use in your markup, based on the object type and database id.  So for example if I had a Post with id 1, I could do:</p>
<pre class="textmate-source sunburst"><span class='linenum'>    1</span> <span class="text text_html text_html_ruby">
<span class='linenum'>    2</span> <span class="meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html"><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html">&lt;</span><span class="entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html">li</span> <span class="meta meta_attribute-with-value meta_attribute-with-value_id meta_attribute-with-value_id_html"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_id entity_other_attribute-name_id_html">id</span><span class="punctuation punctuation_separator punctuation_separator_key-value punctuation_separator_key-value_html">=</span><span class="string string_quoted string_quoted_double string_quoted_double_html"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html">"</span><span class="meta meta_toc-list meta_toc-list_id meta_toc-list_id_html"><span class="source source_ruby source_ruby_embedded source_ruby_embedded_html"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">&lt;%=</span> dom_id<span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">(</span><span class="variable variable_other variable_other_readwrite variable_other_readwrite_instance variable_other_readwrite_instance_ruby"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_ruby">@</span>post</span><span class="punctuation punctuation_section punctuation_section_function punctuation_section_function_ruby">)</span> <span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_ruby">%&gt;</span></span></span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html">"</span></span></span><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html">&gt;</span></span>
</span><span class='linenum'>    3</span> </pre>
<p>And it would be rendered as</p>
<pre class="textmate-source sunburst"><span class='linenum'>    1</span> <span class="text text_html text_html_ruby">
<span class='linenum'>    2</span> <span class="meta meta_tag meta_tag_inline meta_tag_inline_any meta_tag_inline_any_html"><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_begin punctuation_definition_tag_begin_html">&lt;</span><span class="entity entity_name entity_name_tag entity_name_tag_inline entity_name_tag_inline_any entity_name_tag_inline_any_html">li</span> <span class="meta meta_attribute-with-value meta_attribute-with-value_id meta_attribute-with-value_id_html"><span class="entity entity_other entity_other_attribute-name entity_other_attribute-name_id entity_other_attribute-name_id_html">id</span><span class="punctuation punctuation_separator punctuation_separator_key-value punctuation_separator_key-value_html">=</span><span class="string string_quoted string_quoted_double string_quoted_double_html"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_html">"</span><span class="meta meta_toc-list meta_toc-list_id meta_toc-list_id_html">post_1</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_html">"</span></span></span><span class="punctuation punctuation_definition punctuation_definition_tag punctuation_definition_tag_end punctuation_definition_tag_end_html">&gt;</span></span>
</span><span class='linenum'>    3</span> </pre>
<p>This is fine until you use dom_id on an object with a compound name like EmailNotification. Now dom_id will create a unique id like</p>
<p>email_notification_1</p>
<p>Which is still good until I&#8217;m doing some front-end testing and I&#8217;m parsing these dom ids to find their related db records.  Now I need to split &#8220;email_notification_1&#8243; in a way that easily separates the object class from the identifier.  How would you do that? </p>
<p>Well, what we can do here is use an advanced Regular Expression feature called a negative look ahead.  We want to say, &#8220;split the string on the last underscore&#8221;.</p>
<p>A negative look ahead uses the ?! syntax in parenthesis, everything that follows ?! in the parenthesis will be used in the negative look ahead matching.  So in our case we want to say, there should be a underscore that can be followed by any character except another underscore.  The pattern would like like this:</p>
<pre>
/_(?!.*_)/
</pre>
<p>We can confirm in irb</p>

<div class="wp-terminal">$ ree-1.8.7-head > x = "email_notification_1234"<br/>=> "email_notification_1234"<br/>ree-1.8.7-head > klass, id = x.split(/_(?!.*_)/)<br/>=> ["email_notification", "1234"] <br/></div>

<p>Likewise, If we wanted to split the dom_id on the first underscore we could just use a regular look ahead.  It is similar but uses the ?= syntax.</p>

<div class="wp-terminal">$ ree-1.8.7-head > x.split(/_(?=.*_)/)<br/>=> ["email", "notification_1234"]<br/></div>

]]></content:encoded>
			<wfw:commentRss>http://www.christopherirish.com/2010/10/15/splitting-rails-dom-ids-with-regex-look-aheads/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
