如果存在于字符串中,则替换回正斜杠的问题 [英] issue with replacing back to forward slash if it exists in string

查看:49
本文介绍了如果存在于字符串中,则替换回正斜杠的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决以下问题:

I am struggling with the following:

    if(typeof value == "string" && value.match(/\/)){
        value = value.replace(/\\/g, "/");
    }

我要实现的目的是查找字符串是否包含任何反斜杠 \ .如果是,我需要将其替换为正斜杠,但以上操作不起作用,并给我编译错误.感谢您的帮助

what I am trying to achieve is to find if string contains any back slash \. if yes I need to replace it with forward slash but the above does not work and gives me compilation error. Any help is appreciated

推荐答案

虽然替换字符串中的转义字符可能没有意义,但是您可以使用

While it may not make sense to replace escaped characters in a string, you could use String.raw to ensure the escaped characters are treated as literals.

摘要:

let value = String.raw`luke\ abcd \n \t ssas\.`;

if (typeof value === "string" && value.match(/\\/)) {
  value = value.replace(/\\/g, "/");
}

console.info(value);

这篇关于如果存在于字符串中,则替换回正斜杠的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆