admin 管理员组文章数量: 1086019
I have a string for example:
var string = 'This is a text that needs to change';
And then I have two arrays.
var array1 = new Array('a', 'e', 'i', 'o', 'u');
var array2 = new Array('1', '2', '3', '4', '5');
Now, what I what to do is check string with array1 and replace the string with corresponding value from array 2. So with a function to do this I need to get something like:
string = 'Th3s 3s 1 t2xt th1t n22ds to ch1ng2';
Any ideas on how to approach this problem? And may be an efficient approach? Since I plan to use this on huge chunks of data.
EDIT:
Based on the answers here I have piled a code to allow the above operations while also allowing few special characters. Check it out.
var string = 'This is a text that needs to change';
var array1 = new Array('ee', 'a', 'e', 'i', 'o', ']');
var array2 = new Array('!', '1', '2', '3', '4', '5');
function escapeString(str){
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
var re = new RegExp('(' + escapeString(array1.join('ૐ')) + ')', 'g');
var nx = new RegExp(re.source.replace(/ૐ/g, "|"), 'g');
alert(nx);
var lookup = {};
for (var i = 0; i < array1.length; i++) {
lookup[array1[i]] = array2[i];
}
string = string.replace(nx, function(c){
return lookup[c]
});
alert(string);
I have a string for example:
var string = 'This is a text that needs to change';
And then I have two arrays.
var array1 = new Array('a', 'e', 'i', 'o', 'u');
var array2 = new Array('1', '2', '3', '4', '5');
Now, what I what to do is check string with array1 and replace the string with corresponding value from array 2. So with a function to do this I need to get something like:
string = 'Th3s 3s 1 t2xt th1t n22ds to ch1ng2';
Any ideas on how to approach this problem? And may be an efficient approach? Since I plan to use this on huge chunks of data.
EDIT:
Based on the answers here I have piled a code to allow the above operations while also allowing few special characters. Check it out.
var string = 'This is a text that needs to change';
var array1 = new Array('ee', 'a', 'e', 'i', 'o', ']');
var array2 = new Array('!', '1', '2', '3', '4', '5');
function escapeString(str){
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
var re = new RegExp('(' + escapeString(array1.join('ૐ')) + ')', 'g');
var nx = new RegExp(re.source.replace(/ૐ/g, "|"), 'g');
alert(nx);
var lookup = {};
for (var i = 0; i < array1.length; i++) {
lookup[array1[i]] = array2[i];
}
string = string.replace(nx, function(c){
return lookup[c]
});
alert(string);
Share
Improve this question
edited Jan 28, 2014 at 11:41
pewpewlasers
asked Jan 27, 2014 at 23:46
pewpewlaserspewpewlasers
3,2254 gold badges33 silver badges63 bronze badges
3
- Take a look at stackoverflow./questions/2064047/… – Barbara Laird Commented Jan 27, 2014 at 23:49
-
You are searching for a PHP
str_replace
JavaScript equivalent, something like phpjs/functions/str_replace :output = str_replace( array1, array2, input )
– feeela Commented Jan 27, 2014 at 23:50 - possible duplicate of Replacing letters in a string using two arrays? – georg Commented Jan 28, 2014 at 1:28
6 Answers
Reset to default 4If the characters to replace are just regular letters, and nothing that has a special meaning in a regular expression, then you can make a regular expression that matches only those characters. That allows you to use a single replace with a function that translates those characters:
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 str1 = array1.join('');
var re = new RegExp('[' + str1 + ']', 'g');
string = string.replace(re, function(c){
return array2[str1.indexOf(c)]
});
Demo: http://jsfiddle/Guffa/2Uc92/
for(var x = 0 ; x < array1.length; x++)
string = string.replace(new RegExp(array1[x], "g"), array2[x])
FIDDLE
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<array1.length ; ++i )
lookup[array1[i]] = array2[i];
string.replace(regex, function(c) { return lookup[c]; });
// "Th3s 3s 1 t2xt th1t n22ds t4 ch1ng2"
http://jsfiddle/2twr2/
Assuming your two arrays have the same size:
for(var i = 0; i < array1.length; i++){
mystr = mystr.replace(array1[i], array2[i]);
}
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
For the purpose of exploring other interesting methods of doing the same thing, here is an implementation using array map.
It's just another cool way of doing it, without using a loop, replace or regexp.
var string = 'This is a text that needs to change';
var array1 = ['a', 'e', 'i', 'o', 'u'];
var array2 = ['1', '2', '3', '4', '5'];
var a = string.split('');
a.map(function(c) {
if (array1.indexOf(c) != -1) {
a[ a.indexOf(c) ] = array2[ array1.indexOf(c) ];
}
});
var newString = a.join('');
alert( newString );
//Outputs "Th3s 3s 1 t2xt th1t n22ds t4 ch1ng2"
Demo: JSFiddle
Interesting blog post about the array methods - map and reduce.
I'd love to hear thoughts about performance of array map vs the other methods.
本文标签: javascriptReplace string with values from two arraysStack Overflow
版权声明:本文标题:javascript - Replace string with values from two arrays - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744016737a2519055.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论