0

How can I replace this: " / " from a string.

For example: "one / another one" ----> "one/another one"

It just removes the spaces between the slash.. no the orher spaces.

Thanks.

  • As in, replace `" / "` with `"/"` ? What have you tried? Why didn't it work out? I ask because on the face of it, this is a fairly simple operation, so it feels like there could be something I'm misunderstanding.. – Caius Jard Jun 30 '20 at 11:06

3 Answers3

0

This regexp will strip out any runs of whitespaces around slashes, but keeps the slashes intact.

> "one / another one / yet some more / until the end".replace(/\s*\/\s*/g, '/')
  "one/another one/yet some more/until the end"

If you need to keep e.g. //// intact, use + for the quantifier:

> "one / another one / yet some more / until the end (yet this //// remains)".replace(/\s+\/\s+/g, '/')
  "one/another one/yet some more/until the end (yet this //// remains)"
AKX
  • 152,115
  • 15
  • 115
  • 172
0

You can do this:

console.log("one / another one".replace(/\ \/ /, '/'));
Rahul Bhobe
  • 4,165
  • 4
  • 17
  • 32
0

You did'nt specified the language; In my answer I will be using Python; use - string_name = string_name.replace(' / ','/') (where string_name is name of variable in which you stored the actual string)

'replace()' is used here , in the first argument you provide what you want to replace and in the second argument you provide with what you want to replace

Ujjwal Saxena
  • 121
  • 1
  • 6