Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed a regex to add css class to first and last list item
    text
    copied!<p>UPDATE: Thank you all for your input. Some additional information.</p> <p>It's really just a small chunk of markup (20 lines) I'm working with and had aimed to to leverage a regex to do the work.</p> <p>I also do have the ability to hack up the script (an ecommerce one) to insert the classes as the navigation is built. I wanted to limit the number of hacks I have in place to keep things easier on myself when I go to update to the latest version of the software.</p> <p>With that said, I'm pretty aware of my situation and the various options available to me. The first part of my regex works as expected. I posted really more or less to see if someone would say, "hey dummy, this is easy just change this....."</p> <p>After coming close with a few of my efforts, it's more of the principle at this point. To just know (and learn) a solution exists for this problem. I also hate being beaten by a piece of code.</p> <p>ORIGINAL:</p> <p>I'm trying to leverage regular expressions to add a CSS a class to the first and last list items within an ordered list. I've tried a bunch of different ways but can't produce the results I'm looking for.</p> <p>I've got a regular expression for the first list item but can't seem to figure a correct one out for the last. Here is what I'm working with:</p> <pre><code> $patterns = array('/&lt;ul+([^&lt;]*)&lt;li/m', '/&lt;([^&lt;]*)(?&lt;=&lt;li)(.*)&lt;\/ul&gt;/s'); $replace = array('&lt;ul$1&lt;li class="first"','&lt;li class="last"$2$3&lt;/ul&gt;'); $navigation = preg_replace($patterns, $replace, $navigation); </code></pre> <p>Any help would be greatly appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload