質問

I try to create links when a user write something in tags. But the problem is I cant convert the charset. like ş = s, ğ = g ... Here my code

$(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>';
});
});

How can I convert chars and build my links. At last I want is if first and last char is blank clear it... Ty.

役に立ちましたか?

解決

The jquery html() function expects a String but you are providing a function, you should move the function out of the html call and pass its return value instead. Also, you don't actually use the returnString variable when you are building your link, I think the return statement should read:

return '<a href="/' + returnString + '/">' + returnString + '</a>';

You might also consider just using encodeURI rather than replacing characters manually - this will convert the invalid characters so that are safe for use in a url.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top