How do I replace whitespaces with underscore?

前端 未结 13 2150

I want to replace whitespace with underscore in a string to create nice URLs. So that for example:

\"This should be connected\" becomes \"This_should_be_conn         


        
13条回答
  •  抹茶落季
    2020-11-29 16:25

    OP is using python, but in javascript (something to be careful of since the syntaxes are similar.

    // only replaces the first instance of ' ' with '_'
    "one two three".replace(' ', '_'); 
    => "one_two three"
    
    // replaces all instances of ' ' with '_'
    "one two three".replace(/\s/g, '_');
    => "one_two_three"
    

提交回复
热议问题