多次替换角色

时间:2014-07-14 13:56:09

标签: javascript arrays char str-replace arabic

事实上,我必须在javascript中处理阿拉伯字符,但我想向您展示我想要的一个例子:

我有这个字符:

var char =" KASSAB&#34 ;;

我希望通过替换字母" A"来获得一系列字符。 in" 1" " 2" " 3"每次,如果我有2个字母" A"在一个字母中,它必须是这样的:

K ** 1 ** SS ** 1 ** B,K ** 1 ** SS ** 2 ** B,K ** 1 ** SS ** 3 ** B,K ** 2 * * SS ** 1 ** B,K ** 2 ** SS ** 2 ** B,K ** 2 ** SS ** 3 ** B,K ** 3 ** SS ** 1 ** B ,K ** 3 ** SS ** 2 ** B,K ** 3 ** SS ** 3 ** B

任何建议请:(?

1 个答案:

答案 0 :(得分:0)

这可以使用递归函数完成,如下所示:

var initialInput = 'KASSAB',
    replaceables = ['A', 'a', 'á'],
    replacements = ['**1**', '**2**', '**3**'];

function indexOfFirstReplaceable(input) {
    var firstIndex = input.length;
    for (var i in replaceables) {
        var index = input.indexOf(replaceables[i]);
        if (index !== -1) firstIndex = Math.min(firstIndex, index);
    }
    return firstIndex < input.length ? firstIndex : -1;
}

function replaceAllAs(input) {
    var index = indexOfFirstReplaceable(input);
    if (index === -1) return [input] // Nothing to replace

    var outputs = [];
    for (var i in replacements) {
        var nextInput = input.substr(0, index) + replacements[i] + input.substr(index + 1);
        outputs = outputs.concat(replaceAllAs(nextInput));
    }
    return outputs;
}

console.log(replaceAllAs(initialInput));

将输出

["K**1**SS**1**B", "K**1**SS**2**B", "K**1**SS**3**B", "K**2**SS**1**B", "K**2**SS**2**B", "K**2**SS**3**B", "K**3**SS**1**B", "K**3**SS**2**B", "K**3**SS**3**B"]

编辑:

或者如果您想要更加实用,请使用mapreduce

var initialInput = 'KASSAB',
    replaceables = ['A', 'a', 'á'],
    replacements = ['**1**', '**2**', '**3**'];

function indexOfFirstReplaceable(input) {
    return replaceables.reduce(function (currentMin, replaceable) {
        var index = input.indexOf(replaceable);
        if (index === -1 || currentMin === -1) return Math.max(index, currentMin);
        else return Math.min(currentMin, index);
    }, -1);
}

function replaceAllAs(input) {
    var index = indexOfFirstReplaceable(input);
    if (index === -1) return [input]; // Nothing to replace

    var outputs = replacements.map(function (replacement) {
        var nextInput = input.substr(0, index) + replacement + input.substr(index + 1);
        return replaceAllAs(nextInput)
    });
    return [].concat.apply([], outputs);
}

console.log(replaceAllAs(initialInput));

编辑2:

为了适应可替换项和替换项的重叠,您需要在使用fromIndex时指定indexOf

var initialInput = 'KASSAB',
    replaceables = ['A', 'a', 'á'],
    replacements = ['A', 'a', 'á'];

function indexOfFirstReplaceable(input, fromIndex) {
    return replaceables.reduce(function (currentMin, replaceable) {
        var index = input.indexOf(replaceable, fromIndex);
        if (index === -1 || currentMin === -1) return Math.max(index, currentMin);
        else return Math.min(currentMin, index);
    }, -1);
}

function replaceAllAs(input, fromIndex) {
    var index = indexOfFirstReplaceable(input, fromIndex);
    if (index === -1) return [input]; // Nothing to replace

    var outputs = replacements.map(function (replacement) {
        var nextInput = input.substr(0, index) + replacement + input.substr(index + 1);
        return replaceAllAs(nextInput, index + 1);
    });
    return [].concat.apply([], outputs);
}

console.log(replaceAllAs(initialInput, 0));

将输出

["KASSAB", "KASSaB", "KASSáB", "KaSSAB", "KaSSaB", "KaSSáB", "KáSSAB", "KáSSaB", "KáSSáB"]