Answers.Body: <p>Craft has a filter for "<a href="https://craftcms.com/docs/templating/filters#kebab">Kebab Case</a>", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute.</p>
  
  <pre><code>{{ tag.name|kebab }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-03T06:08:12.860Z
Answers.Text: Perfect answer. Worth noting that `kebab` didn't exist at the time this question was originally asked. It was added on **Jun 2, 2015**, when [Craft 2.4.2664](https://craftcms.com/changelog#build2664) was released.
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-03T01:09:05.507Z
Answers.LastActivityDate: 2015-12-03T01:09:05.507Z
Answers.UserDisplayName: kmgdev
Answers.Body: <p>I'm not sure if this feature is newer than this question but you can also say <code>{{ tag.slug }}</code> to get <code>apples-and-carrots</code>. All letters become lowercase as well so the result is more "scripting-friendly".</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-03T07:41:24.860Z
Answers.Text: This should be the accepted answer.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-13T19:33:12.960Z
Answers.LastActivityDate: 2015-05-13T19:33:12.960Z
Answers.UserDisplayName: Andreas Linnert
Answers.Body: <p>one more example with array parameter in replace method:</p>
  
  <pre><code>{{ "apples and carrots"|replace({" ":"-"}) }}
  {# outputs "apples-and-carrots" #}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-12-05T08:22:27.627Z
Answers.LastActivityDate: 2019-12-05T08:22:27.627Z
Answers.UserDisplayName: Faiyaz Alam
Answers.Body: <p>There are multiple ways to solve this:</p>
  
  <p><strong>Native:</strong></p>
  
  <p><a href="http://twig.sensiolabs.org/doc/filters/url_encode.html">Twigs <code>url_encode</code> filter</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|url_encode }}
  {# outputs "apples%20and%20carrots" #}
  </code></pre>
  
  <p><a href="http://twig.sensiolabs.org/doc/filters/replace.html">Twigs <code>replace</code> filter</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|replace(' ', '-') }}
  {# outputs "apples-and-carrots" #}
  </code></pre>
  
  <p><strong>Plugins:</strong></p>
  
  <p><a href="https://github.com/victor-in/Craft-TwigPCRE">Twig PCRE Filters - Plugin</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|preg_replace('/ /', '-') }}
  {# outputs "apples-and-carrots" #}
  </code></pre>
  
  <p><a href="https://github.com/low/low_regex/">Low Regex - Plugin</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|regex('/ /', '-') }}
  {# outputs "apples-and-carrots" #}
  </code></pre>
  
  <p><a href="https://gist.github.com/boboldehampsink/7354431">Slugify - Plugin</a>:</p>
  
  <pre class="lang-twig prettyprint-override"><code>{{ "apples and carrots"|slugify }}
  {# outputs "apples-and-carrots" #}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T15:31:01.120Z
Answers.Text: If you want to handle "apples & *!@#@! carrots" then you need the slugify plugin
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-15T13:39:36.433Z
Answers.LastActivityDate: 2014-07-15T13:39:36.433Z
Answers.UserDisplayName: Victor
Body: <p>I'm using tags to do some Javascripting.</p>
  
  <p>Is there any way that I can tell Craft to remove whitespace from {{ tag.name }} if the tag.name has spaces (for example 'apples and carrots'), and add dashes ('apples-and-carrots') ?</p>
  
Tags: <tags>
Title: Removing whitespace from {{ tag.name }}
ThreadText: # Removing whitespace from {{ tag.name }}
  
  (Asked by: username_0 on 2014-07-15)
  
  I'm using tags to do some Javascripting.
  
  Is there any way that I can tell Craft to remove whitespace from {{ tag.name }} if the tag.name has spaces (for example 'apples and carrots'), and add dashes ('apples-and-carrots') ?
  
  **Tags:** tags
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2014-07-15. Score: 25 | Accepted answer
  
  There are multiple ways to solve this:
  
  **Native:**
  
  [Twigs `url_encode` filter](http://twig.sensiolabs.org/doc/filters/url_encode.html):
  
  ```twig
  {{ "apples and carrots"|url_encode }}
  {# outputs "apples%20and%20carrots" #}
  
  ```
  
  [Twigs `replace` filter](http://twig.sensiolabs.org/doc/filters/replace.html):
  
  ```twig
  {{ "apples and carrots"|replace(' ', '-') }}
  {# outputs "apples-and-carrots" #}
  
  ```
  
  **Plugins:**
  
  [Twig PCRE Filters - Plugin](https://github.com/username_6-in/Craft-TwigPCRE):
  
  ```twig
  {{ "apples and carrots"|preg_replace('/ /', '-') }}
  {# outputs "apples-and-carrots" #}
  
  ```
  
  [Low Regex - Plugin](https://github.com/low/low_regex/):
  
  ```twig
  {{ "apples and carrots"|regex('/ /', '-') }}
  {# outputs "apples-and-carrots" #}
  
  ```
  
  [Slugify - Plugin](https://gist.github.com/boboldehampsink/7354431):
  
  ```twig
  {{ "apples and carrots"|slugify }}
  {# outputs "apples-and-carrots" #}
  
  ```
  
  > **Comments:**
  > **username_7**: If you want to handle "apples & *!@#@! carrots" then you need the slugify plugin  
  
  ---
  
  ### Answer by username_1 on 2015-12-03. Score: 15
  
  Craft has a filter for "[Kebab Case](https://craftcms.com/docs/templating/filters#kebab)", which I think is what you need. It strips out punctuation and replaces spaces with dashes. Useful if you need to format a field as a HTML attribute.
  
  ```
  {{ tag.name|kebab }}
  
  ```
  
  > **Comments:**
  > **username_2**: Perfect answer. Worth noting that `kebab` didn't exist at the time this question was originally asked. It was added on **Jun 2, 2015**, when [Craft 2.4.2664](https://craftcms.com/changelog#build2664) was released.  
  
  ---
  
  ### Answer by username_3 on 2015-05-13. Score: 9
  
  I'm not sure if this feature is newer than this question but you can also say `{{ tag.slug }}` to get `apples-and-carrots`. All letters become lowercase as well so the result is more "scripting-friendly".
  
  > **Comments:**
  > **username_4**: This should be the accepted answer.  
  
  ---
  
  ### Answer by username_5 on 2019-12-05. Score: 1
  
  one more example with array parameter in replace method:
  
  ```
  {{ "apples and carrots"|replace({" ":"-"}) }}
  {# outputs "apples-and-carrots" #}
  
  ```
  
  
  ---
  
  