Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript replace globally with array

You can use array for replacement:

var array = {"from1":"to1", "from2":"to2"}

for (var val in array)
    text = text.replace(array, array[val]);

But what if you need to replace globally, ie text = text.replace(/from/g, "to");

Array is pretty big, so script will take a lot of space if I write "text = text.replace(...)" for every variable.

How can you use array in that case? "/from1/g":"to1" does not working.

like image 601
Qiao Avatar asked Jan 14 '10 12:01

Qiao


People also ask

How do you replace an array in another array?

How do you replace an array element with another array element? An item can be replaced in an array using two approaches: Method 1: Using splice() method. Method 2: Using array map() and filter() methods.

Are arrays Global in JavaScript?

The Array in JavaScript is a global object which contains a list of items. It is similar to any variable, in that you can use it to hold any type of data. However, it has one important difference: it can hold more than one item of data at a time.

How do you replace an item in an array?

Replace an Element in an Array using Array.Use the indexOf() method to get the index of the element you want to replace. Call the Array. splice() method to replace the element at the specific index. The array element will get replaced in place.

Is replace an array method JavaScript?

Method 1: Using splice() method The array type in JavaScript provides us with splice() method that helps us in order to replace the items of an existing array by removing and inserting new elements at the required/desired index.


3 Answers

var array = {"from1":"to1", "from2":"to2"}

for (var val in array)
    text = text.replace(new RegExp(val, "g"), array[val]);

Edit: As Andy said, you may have to escape the special characters using a script like this one.

like image 147
Fabien Ménager Avatar answered Oct 19 '22 14:10

Fabien Ménager


Here is my solution, assuming the string keys in array need not to be escaped.

It is particularly efficient when the object array is large:

var re = new RegExp(Object.keys(array).join("|"), "g");
var replacer = function (val) { return array[val]; };
text = text.replace(re, replacer);

Note this requires the Object.keys method to be available, but you can easily shim it if it is not.

like image 28
Luc125 Avatar answered Oct 19 '22 13:10

Luc125


Here's the idiom for simple, non-RegExp-based string replace in JS, so you don't need to worry about regex-special characters:

for (var val in array)
    text= text.split(val).join(array[val]);

Note there are issues with using an Object as a general purpose lookup. If someone's monkeyed with the Object prototype (bad idea, but some libraries do it) you can get more val​s than you wanted; you can use a hasOwnProperty test to avoid that. Plus in IE if your string happens to clash with a method of Object such as toString, IE will mysteriously hide it.

For your example here you're OK, but as a general case where the strings can be anything, you'd need to work around it, either by processing the key strings to avoid clashes, or by using a different data structure such as an Array of [find, replace] Arrays.

like image 1
bobince Avatar answered Oct 19 '22 14:10

bobince