如何解决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);
此结果与您的示例中的结果不匹配,但这是因为它与您对要执行的操作的描述不匹配。我执行了说明。
,对于那些不喜欢正则表达式的人...
s = "1111 type reallycoolsentence text.json\n1111 type anotherreallycoolsentence text2.json"
wArray = s.split(" ");
wArray = wArray.filter( value => value !== "1111");
wArray = wArray.filter(value => !value.includes('.json'));
result = wArray.join(" ");
输出:
type reallycoolsentence type anotherreallycoolsentence
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。