0

Here is my array and string:

var array = new Array('üÜ', 'ıI', 'iİ', 'ğĞ', 'şŞ', 'çÇ');
var string = 'İSTANBUL, ÜSKÜDAR, Çarşamba'

I'd to replace every (for ü) to [üÜ]. I mean [üÜ]SK[üÜ]DAR. Can anyone help me?

2 Answers 2

3

You can use replace() method

string.replace(/ü|Ü/g, '[üÜ]')

For all matches,

array.forEach(function(key){
    string = string.replace(new RegExp('['+ key +']', 'g'), '['+ key +']');
});
Sign up to request clarification or add additional context in comments.

2 Comments

how can i replace all chars in array with this method?
I though you can figure out from this part. Ok let me update my answer.
0
function replaceAll(source, search, replace, ignoreCase) {
    //SCAPE SPECIAL CHARACTERES.
    var search1 = search.toString().replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
    //IGNORE CASE SENSIVITY.
    var ignore = (ignoreCase) ? "gi" : "g";
    var result = source.replace(new RegExp(search1, ignore), replace);
    return result;
}


var array = new Array('üÜ', 'ıI', 'iİ', 'ğĞ', 'şŞ', 'çÇ');

for (var i=0; i < array.length; i++){
    array[i] = replaceAll(array[i],"ü", "üÜ",true);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.