RegEx issue with hash tag

前端 未结 3 1684
陌清茗
陌清茗 2020-12-11 19:15

I am trying to match hash tags and wrap them with an anchor tag. Here is the POC:



        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-11 19:45

    You can solve it this way (it's much simpler, actually):

    var content = "I like #redApple. I have a #black hat. #red is my favorite color";
    
    var re = /(#[a-z0-9][a-z0-9\-_]*)/ig;
    
    content = content.replace(re, function(x) { return '' + x + ' '; });
    
    $(".display").append(content);
    

    vSearch = value.replace(/[-\/\\^$*+?.=()|[\]{}]/g, '\\$&'); in your code is redundant - you don't need it since your matches won't have any of these symbols according to regexp you used to get them.

提交回复
热议问题