Replace forward slash “/ ” character in JavaScript string?

前端 未结 8 545
你的背包
你的背包 2020-12-02 15:35

I have this string:

var someString = \"23/03/2012\";

and want to replace all the \"/\" with \"-\".

I tried to do this:

<         


        
相关标签:
8条回答
  • 2020-12-02 15:45

    Try escaping the slash: someString.replace(/\//g, "-");

    By the way - / is a (forward-)slash; \ is a backslash.

    0 讨论(0)
  • 2020-12-02 15:47

    Area.replace(new RegExp(/\//g), '-') replaces multiple forward slashes (/) with -

    0 讨论(0)
  • 2020-12-02 15:49

    Escape it: someString.replace(/\//g, "-");

    0 讨论(0)
  • 2020-12-02 15:56

    Just use the split - join approach:

    my_string.split('/').join('replace_with_this')
    
    0 讨论(0)
  • 2020-12-02 15:58

    You can just replace like this,

     var someString = "23/03/2012";
     someString.replace(/\//g, "-");
    

    It works for me..

    0 讨论(0)
  • 2020-12-02 16:01

    First of all, that's a forward slash. And no, you can't have any in regexes unless you escape them. To escape them, put a backslash (\) in front of it.

    someString.replace(/\//g, "-");
    

    Live example

    0 讨论(0)
提交回复
热议问题