简体   繁体   English

从锚标记中提取锚文本的Javascript

[英]Javascript for extracting anchor text from anchor tag

need help in the following. 在下面需要帮助。

In javascript, need to pass a input 在javascript中,需要传递输入

as eg: 如:

str="<a href=www.google.com>Google</a>"; // this is for example actual input vary
// str is passed as parameter for javascript function

The output should retrieve as 'Google'. 输出应检索为“ Google”。

I have regex in java and it is working fine in it. 我在Java中有正则表达式,并且在其中工作正常。

String regex = "< a [ ^ > ] * > ( . * ? ) < / a > ";
Pattern p = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);

but in javascript it is not working. 但在javascript中无法正常工作。

how can I do this in Javascript. 我该如何用Javascript做到这一点。 Can anyone provide me help for javascript implementation. 谁能为我提供有关JavaScript实现的帮助。

I dont think you would like to use Regex for this. 我不认为您要为此使用Regex。 You may try simply like this:- 您可以这样简单地尝试:-

<a id="myLink" href="http://www.google.com">Google</a>

    var anchor = document.getElementById("myLink");

    alert(anchor.getAttribute("href")); // Extract link

    alert(anchor.innerHTML); // Extract Text

Sample DEMO 样本演示

EDIT:- (As rightly commented by Patrick Evans) 编辑:-(正如Patrick Evans正确评论的那样)

var str = "<a href=www.google.com>Google</a>";
var str1 = document.createElement('str1');
str1.innerHTML = str;
alert(str1.textContent);
alert( str1.innerText);

Sample DEMO 样本演示

Insert the HTML string into an element, and then just get the text ? 将HTML字符串插入元素中,然后获取文本?

var str = "<a href=www.google.com>Google</a>";
var div = document.createElement('div');

div.innerHTML = str;
var txt = div.textContent ? div.textContent : div.innerText;

FIDDLE 小提琴

In jQuery this would be : 在jQuery中,它将是:

var str = "<a href=www.google.com>Google</a>";
var txt = $(str).text();

FIDDLE 小提琴

From the suggestions given by you all I got answer and works for me 从大家的建议中我得到了答案并为我工作

function extractText(){
var anchText = "<a href=www.google.com>Google</a>";
    var str1 = document.createElement('str1');      
    str1.innerHTML = anchText;
    alert("hi "+str1.innerText);
    return anc;
}

Thanks everyone for the support 谢谢大家的支持

Just going to take an initial stab at this, I can update this is you add more tests cases or details to your question: 只是初步了解一下,我可以更新一下,这是您在问题中添加了更多测试用例或详细信息:

\w+="<.*>(.*)</.*>"

This matches your provided example, in addition it doesn't matter if: 这与您提供的示例匹配,此外,是否:

  • the variable name is different 变量名称不同
  • the tag or contents of the tag wrapping the text are different 标签或包含文本的标签内容不同

What will break this, specifically, is if there are angle brackets inside your html tag, which is possible. 特别是,如果您的html标记内有尖括号,则可能会打破此限制。

Note: It is a much better idea to do this using html as other answers have attempted, I only answered this with a regex because that was what OP asked for. 注意:使用html进行此操作是一个更好的主意,因为尝试了其他答案,所以我只使用了正则表达式来回答,因为这是OP的要求。 To OP, if you can do this without a regex, do that instead. 对于OP,如果无需正则表达式即可执行此操作,请改为执行此操作。 You should not attempt to parse HTML with javascript when possible, and this regex is not comparable to a full html parser. 您不应在可能的情况下尝试使用javascript解析HTML,并且此正则表达式无法与完整的html解析器进行比较。

No need for a regex, just parse the string with DOMParser and get the element and then use the DOM object methods/attributes 不需要正则表达式,只需使用DOMParser解析字符串并获取元素,然后使用DOM对象的方法/属性

var parser = new DOMParser();
var str='<a href='www.google.com'>Google</a>"; 
var dom = parser.parseFromString(str,"text/xml");

//From there use dom like you would use document
var atags = dom.getElementsByTagName("a");
console.log( atags[0].textContent );

//Or
var atag = dom.querySelector("a");
console.log( atag.textContent );

//Or
var atag = dom.childNodes[0];
console.log( atag.textContent );

Only catch is DOMParser is not supported in IE lower than 9. IE 9以下版本不支持DOMParser。

Well, if you're using JQuery this should be an easy task. 好吧,如果您使用的是JQuery,这应该很容易。

I would just create an invisible div and render this anchor () on it. 我只是创建一个不可见的div并在其上呈现此anchor()。 Afterwards you could simply select the anchor and get it's inner text. 之后,您可以简单地选择锚点并获取其内部文本。

$('body').append('<div id="invisibleDiv" style="display:none;"></div>'); //create a new invisible div
$('#invisibleDiv').html(str); //Include yours "str" content on the invisible DIV
console.log($('a', '#invisibleDiv').html()); //And this should output the text of any anchor inside that invisible DIV.

Remember, to do this way you must have JQuery loaded on your page. 请记住,要这样做,必须在页面上加载JQuery。

EDIT: Use only if you've already have JQuery on your project, since as stated below, something simple as this should not be a reason for the inclusion of this entire library. 编辑:仅当您已经在项目上具有JQuery时使用,因为如下所述,因为这不应该是将整个库包含在内的简单原因。

Assuming that you are using java, from the provided code. 假设您使用的是Java,请提供的代码。

I would recommend you to use JSoup to extract text inside anchor tag. 我建议您使用JSoup提取锚标记内的文本。
Here's a reason why. 这是一个原因。 Using regular expressions to parse HTML: why not? 使用正则表达式解析HTML:为什么不呢?

String html = "<a href='www.google.com'>Google</a>";
Document doc = Jsoup.parse(html);
Element link = doc.select("a").first();

String linkHref = link.attr("href"); // "www.google.com"
String linkText = link.text(); // "Google""

String linkOuterH = link.outerHtml(); 
// "<a href='www.google.com'>Google</a>";
String linkInnerH = link.html(); // "<b>example</b>"

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

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