在Javascript中的正则表达式删除链接 [英] Regex in Javascript to remove links

查看:120
本文介绍了在Javascript中的正则表达式删除链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中有一个字符串,它包含 a 标记,其中包含 href 。我想删除所有链接文本。我知道如何删除链接并保留内部文本,但我想完全删除链接。

I have a string in JavaScript and it includes an a tag with an href. I want to remove all links and the text. I know how to just remove the link and leave the inner text but I want to remove the link completely.

例如:

var s = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";

我想使用正则表达式,所以我留下:

I would like to use a regex so I'm left with:

s = "check this out. cool, huh?";


推荐答案

这将删除< a 和 / a>

mystr = "check this out <a href='http://www.google.com'>Click me</a>. cool, huh?";
alert(mystr.replace(/<a\b[^>]*>(.*?)<\/a>/i,""));

这不是真的万无一失,但也许它会为你的目的做到这一点......

It's not really foolproof, but maybe it'll do the trick for your purpose...

这篇关于在Javascript中的正则表达式删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆