简体   繁体   English

javascript 如何在主题标签上进行正则表达式匹配和替换,但排除主题标签字符

[英]javascript how to regex match and replace on hashtags but exclude the hashtag character

I have the following function:我有以下 function:

function formattedTitle(posttitle,hreflink) {
  return `<a href='`+ hreflink +`'>` + posttitle.replace(/(^|\s)(#[-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='`+ hreflink + `'>`) + '</a>';
}

When I run当我跑

console.log(formattedTitle('This is #awesome news','google.com'));

It outputs:它输出:

<a href='google.com'>This is </a><a class="hashtag" href='/search?q=hashtag:"#awesome"'>#awesome</a><a href='google.com'> news</a>

 function formattedTitle(posttitle, hreflink) { return `<a href='` + hreflink + `'>` + posttitle.replace(/(^|\s)(#[-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='` + hreflink + `'>`) + '</a>'; } console.log(formattedTitle('This is #awesome news', 'google.com'));

Notice how it includes the "#" in the $2 match.注意它是如何在 $2 匹配中包含“#”的。 How can I exclude the hashtag character in the hashtag: attribute but keep it in between the href value?如何排除hashtag:属性中的主题标签字符,但将其保留在 href 值之间?

So the output should look like:所以 output 应该如下所示:

<a href='google.com'>This is </a><a class="hashtag" href='/search?q=hashtag:"awesome"'>#awesome</a><a href='google.com'> news</a>

I have been able to get it to work by doing another replace on the entire thing to replace '/search?q=hashtag:"# with '/search?q=hashtag:" but I am wondering if it's possible without the second replace?我已经能够通过对整个事物进行另一次替换以替换'/search?q=hashtag:"# with '/search?q=hashtag:"来使其工作,但我想知道是否可以不进行第二次替换?

Move the # outside of the captured 2nd group, so it isn't captured.#移到捕获的第二组之外,因此它不会被捕获。 When replacing, in the href , replace with just $2 (so no hashtag).替换时,在href中,仅替换为$2 (因此没有主题标签)。 When replacing the text inside the <a> , replace with #$2 , so that the hashtag gets added in the proper place:替换<a>中的文本时,请替换为#$2 ,以便将主题标签添加到正确的位置:

 function formattedTitle(posttitle, hreflink) { return `<a href='` + hreflink + `'>` + posttitle.replace(/(^|\s)#([-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>#$2</a><a href='` + hreflink + `'>`) + '</a>'; } console.log(formattedTitle('This is #awesome news', 'google.com'));

You just need to move the hash symbol out of your capture group;您只需要将 hash 符号移出捕获组即可; doing so will not change the matching semantics.这样做不会改变匹配的语义。 Like so:像这样:

function formattedTitle(posttitle, hreflink) {
  return `<a href='`+ hreflink +`'>` + posttitle.replace(/(^|\s)#([-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='`+ hreflink + `'>`) + '</a>';
}

Just add another capturing group so that you get a match with the hashtag (or any other character you'd like to capture) in $2 and another match without the hashtag in $3 : (#([-.\w]+)) rather than (#[-.\w]+) :只需添加另一个捕获组,以便您在$2中获得与主题标签(或您想要捕获的任何其他字符)的匹配,并在$3中获得另一个没有主题标签的匹配: (#([-.\w]+))而不是比(#[-.\w]+)

 function formattedTitle(postTitle, href) { const parts = postTitle.replace( /(^|\s)(#([-.\w]+))/gi, `$1</a><a class="hashtag" href="/search?q=hashtag:$3">$2</a><a href="${ href }">`); return `<a href="${ href }">${ parts }</a>`; } document.getElementById('postTitle').innerHTML = formattedTitle('This is #awesome news', 'https://google.com');
 h3 { font-family: monospace; font-size: 24px; margin: 8px 0; } a { padding: 8px 0; text-decoration: none; display: inline-block; }.hashtag { padding: 6px 8px; border: 3px solid blue; border-radius: 3px; margin: 0 8px; }
 <h3 id="postTitle"></h3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM