如何使用Javascript转换替换函数来替换多个字符?

时间:2012-10-02 17:32:06

标签: php javascript replace

我使用此代码:

<script type="text/javascript">
    function transfer(which) {
        which = which.replace(/[\-]+/g,'-');    // to remove extra hypens
                which = which.replace( 'č', 'c' ); 
                which = which.replace( 'ē', 'e' ); 
                which = which.replace( 'ģ', 'g' ); 
                which = which.replace( 'ī', 'i' ); 
                which = which.replace( 'ķ', 'k' );
                which = which.replace( 'ļ', 'l' ); 
                which = which.replace( 'ņ', 'n' ); 
                which = which.replace( 'ū', 'u' ); 
                which = which.replace( 'ž', 'z' ); 
                which = which.replace( 'š', 's' ); 
                which = which.replace( 'ā', 'a' );  
        which = which.replace(/\s/g,'-'); // to replace spaces with hypens
        which = which.replace(/[\-]+/g,'-');    // to remove extra hypens
        which = which.replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase(); // to convert to lower case 
        document.getElementById("url_slug").value = which;
   }
</script>

如你所见,现在有许多变化的角色,但我还需要将所有俄语角色改为拉丁语。如果我这样做,我将是非常长的代码。 所以我的问题是:是否可以使用Javascript传输替换功能替换多行字符而不是每行一个字符。 与PHP类似的东西

 $cyr  = array('а','б','в','г','д','e','ж','з','и','й','к','л','м','н','о','п','р','с','т','у', 
        'ф','х','ц','ч','ш','щ','ъ','ь', 'ю','я','А','Б','В','Г','Д','Е','Ж','З','И','Й','К','Л','М','Н','О','П','Р','С','Т','У',
        'Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ь', 'Ю','Я' );
        $lat = array( 'a','b','v','g','d','e','zh','z','i','y','k','l','m','n','o','p','r','s','t','u',
        'f' ,'h' ,'ts' ,'ch','sh' ,'sht' ,'a' ,'y' ,'yu' ,'ya','A','B','V','G','D','E','Zh',
        'Z','I','Y','K','L','M','N','O','P','R','S','T','U',
        'F' ,'H' ,'Ts' ,'Ch','Sh' ,'Sht' ,'A' ,'Y' ,'Yu' ,'Ya' );
        $textcyr = str_replace($cyr, $lat, $textcyr);

2 个答案:

答案 0 :(得分:0)

JavaScript中没有内置函数来执行此操作,但您可以非常轻松地创建自己的函数。在对象中创建俄语到拉丁语的映射,然后使用for..in循环遍历该对象。对于循环处理的每个键值对,请执行替换。

function transfer(which) {
    // define a Russian-to-Latin mapping object
    var map = {
                'б':'b',
                'в':'v',
                 // all the rest ...
              };

    // replace non-Latin chars with Latin replacements
    for(cyr in map) {
        var lat = map[cyr];
        which = which.replace(new RegExp(cyr, 'g'), lat);

        // Or without RexExp: 
        //   which = which.split(cyr).join(lat);
    }

    // general cleanup
    which = which.replace(/\s/g,'-')      // spaces to hyphens
                 .replace(/[\-]+/g,'-')   // remove duplicate hyphens
                 .replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();

    // show the result
    document.getElementById("url_slug").value = which;
}

请注意,当replace与字符串一起用作第一个参数时,它只替换字符串的第一个实例(例如,"aaa".replace('a','b')返回"baa",而不是"bbb" })。您需要使用全局正则表达式多次替换字符串,就像我在示例中所做的那样。只需确保您的俄语没有任何特殊的正则表达式字符,例如^$()等等(但是从您的示例中,我&# 39;猜测这不会成为问题。 另外,您可以使用我已添加的split / join解决方案作为评论。

根据您的需要,您可以将map对象作为参数提供,而不是将其内置到函数中。

答案 1 :(得分:0)

朋友,javascript中没有内置方法,例如str_replace ...但您可以通过创建此名称中的函数来创建此方法...将此函数附加到您的javascript文件或下面的函数中您的html页面中的script标记...

function str_replace(search, replace, subject){ // same as php
    for(var i=0;i<search.length;i++){
        subject=subject.replace(new RegExp(search[i],"g"),replace[i]);
    }
    return subject;
}

使用方法:

var cyr = ['а','б','в','г','д','e','ж','з','и','й','к','л','м',
           'н','о','п','р','с','т','у','ф','х','ц','ч','ш','щ',
           'ъ','ь','ю','я','А','Б','В','Г','Д','Е','Ж','З','И',
           'Й','К','Л','М','Н','О','П','Р','С','Т','У','Ф','Х',
           'Ц','Ч','Ш','Щ','Ъ','Ь', 'Ю','Я'],
    lat = ['a','b','v','g','d','e','zh','z','i','y','k','l','m',
           'n','o','p','r','s','t','u','f','h','ts','ch','sh',
           'sht','a','y','yu','ya','A','B','V','G','D','E','Zh',
           'Z','I','Y','K','L','M','N','O','P','R','S','T','U',
           'F' ,'H' ,'Ts','Ch','Sh','Sht','A','Y','Yu','Ya'];

textcyr = str_replace(cyr, lat, textcyr);

如果您想将方法名称str_replace更改为其他内容,只需更改函数名称...