我使用这段代码:
<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);