Replace string with values from two arrays

前端 未结 6 1677
渐次进展
渐次进展 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:32

    Here's an example:

    var string = 'This is a text that needs to change';
    
    var vowels = ['a','e','i','o','u'];
    var numbers = [1,2,3,4,5];
    
    var result = string.replace(/./g, function(char) {
      var idx = vowels.indexOf(char);
      return idx > -1 ? numbers[idx] : char;
    });
    //^ Th3s 3s 1 t2xt th1t n22ds t4 ch1ng2
    

提交回复
热议问题