# How to replace backslash in jsonnet?

**URL:** <https://drone.discourse.group/t/how-to-replace-backslash-in-jsonnet/7748>\
**Category:** Drone Support\
**Created:** [April 8, 2019, 4:22pm UTC](https://drone.discourse.group/t/how-to-replace-backslash-in-jsonnet/7748 "2019-04-08T16:22:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ManuelLR](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/manuellr/32/4323_2.png) [@ManuelLR](https://drone.discourse.group/u/ManuelLR)\
**Post date:** [April 8, 2019, 4:22pm UTC](https://drone.discourse.group/t/how-to-replace-backslash-in-jsonnet/7748/1 "2019-04-08T16:22:41Z")

</div>

First of all I want to thank you for the work of developing this tool.  
Well, I’ll tell you the problem. I’m using jsonnet files and when I try to use substitutions I can’t escape the “/” character.

**Example 1** :

```
local BuildContainer() = {
  kind: "pipeline",
  name: "build_ci_docker",
  steps: [
    {
      name: "docker_tags",
      image: "debian:stable-slim",
      commands: [
        |||
          echo -n "${DRONE_COMMIT_SHA},${DRONE_COMMIT_BRANCH/\//__},ci-${DRONE_BUILD_NUMBER}" > .tags
        |||,
      ],
      // Must generate: /\//__
    },
    {
      name: "build_docker",
      image: "plugins/gcr",
      settings: {
        dry_run: true,
        repo: "eu.gcr.io/project_name/image_name/",
        registry: "eu.gcr.io",
        ignore_missing: true,
        cache_from: [
          "eu.gcr.io/project_name/image_name/" + ":${DRONE_COMMIT_BRANCH/\//__}",
        ],
      },
    },
  ]
};

[
  BuildContainer(),
]

```

**Result 1:**

```
kind: pipeline
name: build_ci_docker

platform:
  os: linux
  arch: amd64

steps:
- name: docker_tags
  image: debian:stable-slim
  commands:
  - "echo -n \"${DRONE_COMMIT_SHA},${DRONE_COMMIT_BRANCH/\\//__},ci-${DRONE_BUILD_NUMBER}\" > .tags\n"

- name: build_docker
  image: plugins/gcr
  settings:
    cache_from:
    - "eu.gcr.io/project_name/image_name/:${DRONE_COMMIT_BRANCH///__}"
    dry_run: true
    ignore_missing: true
    registry: eu.gcr.io
    repo: eu.gcr.io/project_name/image_name/
...

```

**Example 2** :

```
local BuildContainer() = {
  kind: "pipeline",
  name: "build_ci_docker",
  steps: [
    {
      name: "docker_tags",
      image: "debian:stable-slim",
      commands: [
        |||
          echo -n "${DRONE_COMMIT_SHA},${DRONE_COMMIT_BRANCH/\\//__},ci-${DRONE_BUILD_NUMBER}" > .tags
        |||,
      ],
      // Must generate: /\//__
    },
    {
      name: "build_docker",
      image: "plugins/gcr",
      settings: {
        dry_run: true,
        repo: "eu.gcr.io/project_name/image_name/",
        registry: "eu.gcr.io",
        ignore_missing: true,
        cache_from: [
          "eu.gcr.io/project_name/image_name/" + ":${DRONE_COMMIT_BRANCH/\\//__}",
        ],
      },
    },
  ]
};

[
  BuildContainer(),
]

```

**Result 2** :

```
kind: pipeline
name: build_ci_docker

platform:
  os: linux
  arch: amd64

steps:
- name: docker_tags
  image: debian:stable-slim
  commands:
  - "echo -n \"${DRONE_COMMIT_SHA},${DRONE_COMMIT_BRANCH/\\\\//__},ci-${DRONE_BUILD_NUMBER}\" > .tags\n"

- name: build_docker
  image: plugins/gcr
  settings:
    cache_from:
    - "eu.gcr.io/project_name/image_name/:${DRONE_COMMIT_BRANCH/\\//__}"
    dry_run: true
    ignore_missing: true
    registry: eu.gcr.io
    repo: eu.gcr.io/project_name/image_name/
...

```

In neither of the two cases of each example the substitution of the character “/” in the name of the branch is accomplished. Any idea?.

Thank you very much

---

<div class="post-metadata">

**Author:** ![narze](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/narze/32/4982_2.png) [@narze](https://drone.discourse.group/u/narze)\
**Post date:** [July 1, 2019, 10:22am UTC](https://drone.discourse.group/t/how-to-replace-backslash-in-jsonnet/7748/2 "2019-07-01T10:22:19Z")

</div>

I tried everything involved backslashes and it does not work.  
Finally use another regex which does not use backslashes to solve this problem

```
- "${DRONE_BRANCH//[^a-zA-Z0-9]/-}"; # Replace all non-alphanumerics with hyphens
```

---

<div class="post-metadata">

**Author:** ![songtianyi](https://avatars.discourse-cdn.com/v4/letter/s/b5ac83/32.png) [@songtianyi](https://drone.discourse.group/u/songtianyi)\
**Post date:** [September 3, 2019, 7:16am UTC](https://drone.discourse.group/t/how-to-replace-backslash-in-jsonnet/7748/3 "2019-09-03T07:16:35Z")

</div>

```auto
docker build -t hub.sky-cloud.net/nap/nap-executor:${DRONE_BRANCH//[^a-zA-Z0-9.]/-}_build-${DRONE_BUILD_NUMBER} .

```

not work ☹
