0

我使用这段代码:

<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);
4

2 回答 2

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")。您需要使用全局正则表达式多次替换字符串,就像我在示例中所做的那样。只需确保您的俄语没有任何特殊的正则表达式字符,如^, $, (,)等(但从您的示例来看,我猜这不会有问题)。或者,您可以使用我作为评论添加的split/解决方案。join

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

于 2012-10-02T17:42:03.980 回答
0

朋友,在 javascript 中没有类似的内置方法str_replace...但是您可以通过使用此名称创建一个函数来创建此方法...在您的 javascript 文件中或script在您的 html 页面的标签下附加此函数...

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其他名称,只需更改函数名称...

于 2012-10-03T08:46:39.993 回答