# replace-string

## Interface

```typescript
interface parser {
  name: 'replace-string';
  options: ({
    all?: string | {
      regex: string | { pattern: string, flags?: 'g' & 'm' & 'i' },
      replaceBy: string
    }
  } | 
  {
    group?: string | {
      regex: string | { pattern: string, flags?: 'g' & 'm' & 'i' },
      replaceBy: string
    };
    collection?: string | {
      regex: string | { pattern: string, flags?: 'g' & 'm' & 'i' },
      replaceBy: string
    };
    token?: string | {
      regex: string | { pattern: string, flags?: 'g' & 'm' & 'i' },
      replaceBy: string
    }
  }) & { applyTo?: SDTFQuery }
}
```

## Options

<table><thead><tr><th width="170">Parameter</th><th width="127.33333333333331">Required</th><th width="330">Type</th><th width="179">Default</th><th width="253">Description</th></tr></thead><tbody><tr><td><code>all</code></td><td><code>false</code></td><td><pre class="language-typescript"><code class="lang-typescript">string | {
  regex: string | { pattern: string, flags: 'g' &#x26; 'm' &#x26; 'i' },
  replaceBy: string
}
</code></pre></td><td></td><td>Select all collections, groups and tokens in your SDTF token graph.</td></tr><tr><td><code>group</code></td><td><code>false</code></td><td><pre class="language-typescript"><code class="lang-typescript">string | {
  regex: string | { pattern: string, flags: 'g' &#x26; 'm' &#x26; 'i' },
  replaceBy: string
}
</code></pre></td><td></td><td>Select all groups in your SDTF token graph.</td></tr><tr><td><code>collection</code></td><td><code>false</code></td><td><pre class="language-typescript"><code class="lang-typescript">string | {
  regex: string | { pattern: string, flags: 'g' &#x26; 'm' &#x26; 'i' },
  replaceBy: string
}
</code></pre></td><td></td><td>Select all collections in your SDTF token graph.</td></tr><tr><td><code>token</code></td><td><code>false</code></td><td><pre class="language-typescript"><code class="lang-typescript">string | {
  regex: string | { pattern: string, flags: 'g' &#x26; 'm' &#x26; 'i' },
  replaceBy: string
}
</code></pre></td><td></td><td>Select all tokens in your SDTF token graph.</td></tr><tr><td>regex</td><td><code>required</code></td><td><code>object | string</code></td><td></td><td>If string: the parameter used for the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor">constructor of the regex</a>. If your use case need to use flags prefer object notation.</td></tr><tr><td>regex.pattern</td><td><code>required</code></td><td><code>string</code></td><td></td><td>The pattern of the regex used as first argument of the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor">constructor</a>.</td></tr><tr><td>regex.flags</td><td><code>false</code></td><td><code>string</code></td><td></td><td>The flags to use for regex. In the regex constructor it's the second argument <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#literal_notation_and_constructor">constructor of the regex</a>.</td></tr><tr><td>replaceBy</td><td><code>required</code></td><td><code>string</code></td><td></td><td>The value will used as replacement. <a href="https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/replace">This method</a> is used to apply the replacement.</td></tr><tr><td><code>trim</code></td><td><code>false</code></td><td><code>boolean</code></td><td><code>false</code></td><td>Set true to remove spaces before and after the transformed values. <a href="https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Global_Objects/String/Trim">This method</a> is used to trim.</td></tr><tr><td><code>applyTo</code></td><td><code>false</code></td><td><pre class="language-typescript"><code class="lang-typescript">| { collection: string | true }
| { group: string | true }
| { token: string | true }
| SDTFQuery
</code></pre></td><td></td><td>The selection where to apply the transformation.<br><br><code>collection</code>, <code>group</code>, <code>token</code> take a Regex string or <code>true</code> to select anything of the kind.<br><br>An <a href="../../../guides/querying-a-sdtf-graph#introduction"><code>SDTFQuery</code></a> can be used for advance use cases.</td></tr></tbody></table>

## Basic usage: Rename design token by keeping only characters present after the last slash character (`/`).

{% tabs %}
{% tab title="Input" %}

<pre class="language-json" data-line-numbers><code class="lang-json">{
  "colors": {
<strong>    "Colors/Black": {
</strong>      "$type": "color",
      "$value": {
        "default": {
          "model": "hex",
          "hex": "#000000",
          "alpha": 1
        }
      }
    }
  }
}
</code></pre>

{% endtab %}

{% tab title="Config" %}
{% code title=".specifyrc.json" lineNumbers="true" %}

```json5
{
  "version": "2",
  "repository": "@organization/repository",
  // Only use the personalAccessToken when working with the CLI
  "personalAccessToken": "<your-personal-access-token>",
  "rules": [
    {
      "name": "Rename tokens",
      "parsers": [
        {
          "name": "replace-string",
          "options": {
            "token": {
              "regex": {
                "pattern": "(.*?)\\/",
                "flags": "g"
              },
              "replaceBy": ""
            }
          }
        },
        {
          "name": "to-sdtf",
          "output": {
            "type": "file",
            "filePath": "tokens.json"
          }
        }
      ]
    }
  ]
}
```

{% endcode %}
{% endtab %}

{% tab title="Output" %}

<pre class="language-css" data-title="tokens.json" data-line-numbers><code class="lang-css">{
  "colors": {
<strong>    "Black": {
</strong>      "$type": "color",
      "$value": {
        "default": {
          "model": "hex",
          "hex": "#000000",
          "alpha": 1
        }
      }
    }
  }
}
</code></pre>

{% endtab %}
{% endtabs %}
