Jquery 用href将#标记和@name包装成字符串

Jquery 用href将#标记和@name包装成字符串,jquery,string,replace,Jquery,String,Replace,我正在寻找一种解决方案来替换字符串中的#标记、@names和URL,并用href将这些元素包装起来 我的字符串值 { content: What is role of semantic tech in enabling high-powered #bigdata, #bigdatamgmt, @name, http://bitly.com } 到目前为止我的代码 f_addcontent_links: function(data){ var temp = data,

我正在寻找一种解决方案来替换字符串中的#标记、@names和URL,并用href将这些元素包装起来

我的字符串值

{
content: What is role of semantic tech in enabling high-powered #bigdata, #bigdatamgmt, @name, http://bitly.com
}
到目前为止我的代码

f_addcontent_links: function(data){

    var temp = data,
        hashtag_count = data.match(/#([^\s]+)/g),
        atTag_count = data.match(/@([^\s:]+)/g),
        newtemp, updated_content;

        console.log(hashtag_count);

    if(typeof(temp) !== "undefined" && temp.length > -1){

        for (var i = 0; i <= hashtag_count.length; i++) {
            newtemp = temp.replace(/#([^\s]+)/g, '<a href="//twitter.com/search?q='+hashtag_count+'">'+hashtag_count+'</a>');
                // .replace(/@([^\s:]+)/g, '<a href="//twitter.com/'+atTag_count[i]+'">'+atTag_count[i]+'</a>');
        }
        delete temp;
    }
    return newtemp;
},
f\u addcontent\u链接:函数(数据){
var temp=数据,
hashtag_count=data.match(/#([^\s]+)/g),
atTag_count=data.match(/@([^\s:]+)/g),
新时代,更新内容;
console.log(hashtag_count);
如果(温度类型)!=“未定义”和温度长度>-1){
对于(var i=0;i
注意:这不是我认为你想要的……你需要编辑你的
for
循环。请查看你的
匹配
调用的内容

JS

function f_addcontent_links(data){
    var temp = data,
        hashtag_count = data.match(/[^#]+([^,]+)/ig),
        atTag_count = data.match(/[^@]+([^,]+)/ig),
        newtemp, updated_content;

        console.log(hashtag_count);

    if(typeof(temp) !== "undefined" && temp.length > -1){
        for (var i = 0; i <= hashtag_count.length; i++) {
            newtemp = temp.replace(/[^#]+([^,]+)/ig, '<a href="//twitter.com/search?q='+hashtag_count+'">'+hashtag_count+'</a>');
        }
    }
    return newtemp;
}

window.onload=function(){
  console.log(f_addcontent_links("{content: What is role of semantic tech in enabling high-powered #bigdata, #bigdatamgmt, @name, http://bitly.com}"));
}


这将找到@elements

[^#]+([^,]+)
[^@]+([^,]+)


我使用regex找到了解决方案。它从数组中获取了我的值,并更新了JSON对象的content attr中的所有字符串内容

f_mod_content: function(data){          
            var update_content = data.replace(/(http(s)*\:\/\/[^\s]+\s*)/g, "<a href=\"$1\">$1</a>")
                                .replace(/#([^\s]+)/g, '<a href="//twitter.com/search?q=%23$1">#$1</a>')
                                .replace(/@([^\s:]+)/g, '<a href="//twitter.com/$1">@$1</a>');
                return update_content;
        },
f_mod_内容:函数(数据){
var update\u content=data.replace(/(http(s)*\:\/\/\/[^\s]+\s*)/g,“”)
.替换(/#([^\s]+)/g',)
.替换(/@([^\s::+)/g,”);
返回更新内容;
},

出于好奇,为什么要在那里使用
delete
呢?你没有闭包,delete用于从对象中删除,而不是从对象本身,这可能只会返回false。@NicoSantangelo是的,我删除了它。你不能
删除
变量。delete temp正在删除内容属性。我正在传递数据。content[i]作为函数的一个参数,如果data.content[i]具有#、@或任何href url,我希望将其包装在href标记内。我不希望从@name提取“name”。我希望将现有的“@name”替换为