Replace multiple characters in one replace call

前端 未结 15 2579
失恋的感觉
失恋的感觉 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: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;
    }
    

提交回复
热议问题