String 在一些标记之间建立链接

String 在一些标记之间建立链接,string,text,hyperlink,turkish,String,Text,Hyperlink,Turkish,当用户在标记中写入内容时,我尝试创建链接。但问题是我不能转换字符集。比如ş=s,ğ=g。。。这是我的密码 $(document).ready(function () { $('em').html(function(i, linkle) { var returnString = linkle.toLowerCase(); //Convert Characters returnString = returnString.replace(/ö/g, 'o'); returnString = r

当用户在标记中写入内容时,我尝试创建链接。但问题是我不能转换字符集。比如ş=s,ğ=g。。。这是我的密码

$(document).ready(function () {
   $('em').html(function(i, linkle) {

var returnString = linkle.toLowerCase();
//Convert Characters
returnString = returnString.replace(/ö/g, 'o');
returnString = returnString.replace(/ç/g, 'c');
returnString = returnString.replace(/ş/g, 's');
returnString = returnString.replace(/ı/g, 'i');
returnString = returnString.replace(/ğ/g, 'g');
returnString = returnString.replace(/ü/g, 'u');  

// if there are other invalid chars, convert them into blank spaces
returnString = returnString.replace(/[^a-z0-9\s-]/g, "");
// convert multiple spaces and hyphens into one space       
returnString = returnString.replace(/[\s-]+/g, " ");
// trims current string
returnString = returnString.replace(/^\s+|\s+$/g,"");
// cuts string (if too long)
if(returnString.length > maxLength)
returnString = returnString.substring(0,maxLength);
// add hyphens
returnString = returnString.replace(/\s/g, "-");    

return '<a href="/' + linkle + '/">' + linkle + '</a>';
});
});
$(文档).ready(函数(){
$('em').html(函数(i,linkle){
var returnString=linkle.toLowerCase();
//转换字符
returnString=returnString.replace(/ö/g,'o');
returnString=returnString.replace(/ç/g,'c');
returnString=returnString.replace(/ş/g,'s');
returnString=returnString.replace(/ı/g,'i');
returnString=returnString.replace(/ğ/g,'g');
returnString=returnString.replace(/ü/g,'u');
//如果存在其他无效字符,请将其转换为空格
returnString=returnString。替换(/[^a-z0-9\s-]/g,”);
//将多个空格和连字符转换为一个空格
returnString=returnString.replace(/[\s-]+/g,”);
//修剪当前字符串
returnString=returnString.replace(/^\s+|\s+$/g,“”);
//剪切字符串(如果太长)
如果(returnString.length>maxLength)
returnString=returnString.substring(0,maxLength);
//添加连字符
returnString=returnString.replace(/\s/g,“-”);
返回“”;
});
});
如何转换字符并建立链接。最后我想要的是,如果第一个和最后一个字符是空的,请清除它。。。Ty.

jquery
html()
函数需要一个字符串,但如果您提供了一个函数,则应将该函数移出
html
调用,并传递其返回值。另外,在构建链接时,实际上没有使用
returnString
变量,我认为return语句应该是:

return '<a href="/' + returnString + '/">' + returnString + '</a>';
返回“”;

你也可以考虑只使用而不是手动替换字符——这将转换无效字符,以便在URL中使用是安全的。