javascript - Javascript 使用 RegEXP 删除(但不包括)特殊字符之间的字符
问题描述
我有一个字符串如下:
var s = "1111 type reallycoolsentence\text.json\n1111 type anotherreallycoolsentence text2.json
我试图摆脱反斜杠之间的字符。
想要的结果:
s = "type reallycoolsentence\\type anotherreallycoolsentence"
我知道如何在不删除特殊字符的情况下删除两个特殊字符之间的字符以外的所有内容。堆栈上的每个答案也包括删除它们:(
解决方案
将反斜杠放在替换字符串中。
请注意,您需要将它们加倍以获得文字反斜杠,因为反斜杠是字符串文字中的转义前缀。
var s = "1111 type reallycoolsentence\\text.json\\n1111 type anotherreallycoolsentence text2.json";
var result = s.replace(/\\.*\\/, '\\\\');
console.log(result);
此结果与您的示例中的结果不匹配,但那是因为它与您对您想要做什么的描述不匹配。我实现了描述。
推荐阅读
- xml - ASP.NET Core 的 XML 配置提供程序中的数组
- vba - 根据下拉值选择更改内容控件属性文本样式
- c++ - 模型类无法在 DirectX 10 中初始化
- typescript - 在 Object.assign 上出现 TypeScript 编译错误,但一切正常
- java - 我如何在所有活动中保持相同的歌曲播放
- html - 如何使图像的高度取决于 div 内的其他内容?
- jhipster - 如何从 springboot 应用程序 CF 集群向 Prometheus 公开指标
- kubernetes - Kompose 和 compose-on-kubernetes 之间的区别
- python - 在熊猫数据框中排除超过 500 年的日期
- c# - C# Lists,从对象列表创建列表对象