How can I perform a str_replace in JavaScript, replacing text in JavaScript?

后端 未结 22 1796
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 00:29

I want to use str_replace or its similar alternative to replace some text in JavaScript.

var text = \"this is some sample text that i want to re         


        
相关标签:
22条回答
  • You have the following options:

    1. Replace the first occurrence

    var text = "this is some sample text that i want to replace and this i WANT to replace as well.";
    var new_text = text.replace('want', 'dont want');
    // new_text is "this is some sample text that i dont want to replace and this i WANT to replace as well"
    console.log(new_text)

    1. Replace all occurrences - case sensitive

    var text = "this is some sample text that i want to replace and this i WANT to replace as well.";
    var new_text = text.replace(/want/g, 'dont want');
    // new_text is "this is some sample text that i dont want to replace and this i WANT to replace as well
    console.log(new_text)

    1. Replace all occurrences - case insensitive

    var text = "this is some sample text that i want to replace and this i WANT to replace as well.";
    var new_text = text.replace(/want/gi, 'dont want');
    // new_text is "this is some sample text that i dont want to replace and this i dont want to replace as well
    console.log(new_text)

    More info -> here

    0 讨论(0)
  • 2020-12-01 01:06

    The code that others are giving you only replace one occurrence, while using regular expressions replaces them all (like @sorgit said). To replace all the "want" with "not want", us this code:

    var text = "this is some sample text that i want to replace";
    var new_text = text.replace(/want/g, "dont want");
    document.write(new_text);
    

    The variable "new_text" will result in being "this is some sample text that i dont want to replace".

    To get a quick guide to regular expressions, go here:
    http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
    To learn more about str.replace(), go here:
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace
    Good luck!

    0 讨论(0)
  • 2020-12-01 01:06

    In ECMAScript 2021, you can use replaceAll can be used.

    const str = "string1 string1 string1"
    const newStr = str.replaceAll("string1", "string2");
    
    console.log(newStr)
    //  "string2 string2 string2"
    
    0 讨论(0)
  • 2020-12-01 01:07

    More simply:

    city_name=city_name.replace(/ /gi,'_');
    

    Replaces all spaces with '_'!

    0 讨论(0)
  • 2020-12-01 01:07

    You should write something like that :

    var text = "this is some sample text that i want to replace";
    var new_text = text.replace("want", "dont want");
    document.write(new_text);
    
    0 讨论(0)
  • 2020-12-01 01:09

    hm.. Did you check replace() ?

    Your code will look like this

    var text = "this is some sample text that i want to replace";
    var new_text = text.replace("want", "dont want");
    document.write(new_text);
    
    0 讨论(0)
提交回复
热议问题