我有这样的代码,我在控制台中得到“Uncaught TypeError: Cannot call method 'replace' of undefined”,我不太擅长 javascript
<script>
parse_tag = function (str) {
var create_link = function (url, text) {
var link = $("<a>", {
text: text,
href: url,
target: "_blank"
});
return link.prop('outerHTML');
};
// parse username
str = str.replace(/[@]+[A-Za-z0-9_]+/g, function (s) {
return create_link("http://twitter.com/" + s.replace('@', ''), s);
});
// parse hashtags
str = str.replace(/[#]+[A-Za-z0-9_]+/g, function (s) {
return create_link("http://search.twitter.com/search?q=" + s.replace('#', ''), s);
});
return str;
};
$(document).ready(function() {
var text = $('.desc');
parse_tag(text);
});
</script>