Replace multiple characters in one replace call

前端 未结 15 2298
失恋的感觉
失恋的感觉 2020-11-22 17:24

Very simple little question, but I don\'t quite understand how to do it.

I need to replace every instance of \'_\' with a space, and every instance of \'#\' with no

相关标签:
15条回答
  • 2020-11-22 17:43

    I don't know if how much this will help but I wanted to remove <b> and </b> from my string

    so I used

    mystring.replace('<b>',' ').replace('</b>','');
    

    so basically if you want a limited number of character to be reduced and don't waste time this will be useful.

    0 讨论(0)
  • 2020-11-22 17:43

    Please try:

    • replace multi string

      var str = "http://www.abc.xyz.com"; str = str.replace(/http:|www|.com/g, ''); //str is "//.abc.xyz"

    • replace multi chars

      var str = "a.b.c.d,e,f,g,h"; str = str.replace(/[.,]/g, ''); //str is "abcdefgh";

    Good luck!

    0 讨论(0)
  • 2020-11-22 17:47

    Here's a simple way to do it without RegEx.
    You can prototype and/or cache things as desired.

    // Example: translate( 'faded', 'abcdef', '123456' ) returns '61454'
    function translate( s, sFrom, sTo ){
        for ( var out = '', i = 0; i < s.length; i++ ){
            out += sTo.charAt( sFrom.indexOf( s.charAt(i) ));
        }
        return out;
    }
    
    0 讨论(0)
  • 2020-11-22 17:51

    You can also pass a RegExp object to the replace method like

    var regexUnderscore = new RegExp("_", "g"); //indicates global match
    var regexHash = new RegExp("#", "g");
    
    string.replace(regexHash, "").replace(regexUnderscore, " ");
    

    Javascript RegExp

    0 讨论(0)
  • 2020-11-22 17:52

    You could also try this :

    function replaceStr(str, find, replace) {
        for (var i = 0; i < find.length; i++) {
            str = str.replace(new RegExp(find[i], 'gi'), replace[i]);
        }
        return str;
    }
    
    var text = "#here_is_the_one#";
    var find = ["#","_"];
    var replace = ['',' '];
    text = replaceStr(text, find, replace);
    console.log(text);
    

    find refers to the text to be found and replace to the text to be replaced with

    This will be replacing case insensitive characters. To do otherway just change the Regex flags as required. Eg: for case sensitive replace :

    new RegExp(find[i], 'g')
    
    0 讨论(0)
  • 2020-11-22 17:52

    Here is a "safe HTML" function using a 'reduce' multiple replacement function (this function applies each replacement to the entire string, so dependencies among replacements are significant).

    // Test:
    document.write(SafeHTML('<div>\n\
        x</div>'));
    
    function SafeHTML(str)
        {
        const replacements = [
            {'&':'&amp;'},
            {'<':'&lt;'},
            {'>':'&gt;'},
            {'"':'&quot;'},
            {"'":'&apos;'},
            {'`':'&grave;'},
            {'\n':'<br>'},
            {' ':'&nbsp;'}
            ];
        return replaceManyStr(replacements,str);
        } // HTMLToSafeHTML
    
    function replaceManyStr(replacements,str)
        {
        return replacements.reduce((accum,t) => accum.replace(new RegExp(Object.keys(t)[0],'g'),t[Object.keys(t)[0]]),str);
        }
    
    0 讨论(0)
提交回复
热议问题