Replace string with values from two arrays

前端 未结 6 1615
渐次进展
渐次进展 2021-01-16 07:09

I have a string for example:

var string = \'This is a text that needs to change\';

And then I have two arrays.

var array1 =         


        
6条回答
  •  离开以前
    2021-01-16 07:52

    This sets up 1 RegExp and calls replace only once.

    var string = 'This is a text that needs to change';
    var array1 = new Array('a', 'e', 'i', 'o', 'u');
    var array2 = new Array('1', '2', '3', '4', '5');
    
    var regex = new RegExp( '['+array1.join('')+']', 'g' );
    
    var lookup = {}; // Setup a hash lookup
    for( var i=0 ; i

    http://jsfiddle.net/2twr2/

提交回复
热议问题