首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将字符串中的“/”替换为"one/another one“,如"one / another one”

如何将字符串中的“/”替换为"one/another one“,如"one / another one”
EN

Stack Overflow用户
提问于 2020-06-30 19:05:58
回答 3查看 79关注 0票数 0

如何从字符串中替换这个:“/”。

例如:"one/another one“-> "one / another one”

它只是删除斜杠之间的空格..不是空格。

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2020-06-30 19:07:16

此正则表达式将去掉斜杠周围的所有空格,但保持斜杠不变。

代码语言:javascript
运行
复制
> "one / another one / yet some more / until the end".replace(/\s*\/\s*/g, '/')
  "one/another one/yet some more/until the end"

例如,如果您需要保持////完好无损,请使用+作为量词:

代码语言:javascript
运行
复制
> "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)"
票数 0
EN

Stack Overflow用户

发布于 2020-06-30 19:09:55

您可以这样做:

代码语言:javascript
运行
复制
console.log("one / another one".replace(/\ \/ /, '/'));

票数 0
EN

Stack Overflow用户

发布于 2020-06-30 19:12:25

您没有指定语言;在我的回答中,我将使用Python;使用- string_name = string_name.replace(' / ','/') (其中string_name是存储实际字符串的变量的名称)

这里使用' replace ()‘,在第一个参数中提供要替换的内容,在第二个参数中提供要替换的内容

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62656044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档