在string replace()回调函数中看不到p1,试图替换hashtags

时间:2015-07-23 02:47:29

标签: javascript regex string replace

我试图在某些文字中显示主题标签。在replace()回调myArray内,无法按预期看到p1或p2。

这是一个JSFiddle:http://jsfiddle.net/6c0qw69a/7/

var myTempContent = "this text not showing #hash1 nor here #has2 here either. Only #theseHashTags #show nothing here";
var hashTagRegEx = /(#[a-z\d-]+)/ig;

var myArray = [];
var index = 0;

var myTweetContent = myTempContent.replace(hashTagRegEx, function(match, p1, p2) {

    // Output shows value of p2, but NOT p1 ???
    myArray[index] = p1 + "<a href='#'>" + p2 + "</a>";
    index++;

    return (     
        p1 + "<a href='#'>" + p2 + "</a>"
    );
});

// Output doesn't show correct p2, hash # should be inside <a> tag.
console.log("myTweetContent = " + myTweetContent);

// Why is p1 not showing? Also same issue above with p2.
console.log("myArray = " + myArray);

0 个答案:

没有答案